0% found this document useful (0 votes)
7 views2 pages

Bucket Sort

The document provides instructions for implementing the Bucket Sort algorithm in Java, including a sample code that sorts an array of floating-point numbers. It details the steps involved in creating buckets, distributing elements, sorting each bucket, and concatenating the results. The expected output shows the original and sorted arrays of numbers.

Uploaded by

cocic79333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Bucket Sort

The document provides instructions for implementing the Bucket Sort algorithm in Java, including a sample code that sorts an array of floating-point numbers. It details the steps involved in creating buckets, distributing elements, sorting each bucket, and concatenating the results. The expected output shows the original and sorted arrays of numbers.

Uploaded by

cocic79333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Instruction: Implementing and Testing Bucket Sort in Java

Objective:

To implement the Bucket Sort algorithm in Java and test it on a sample array of floating-point
numbers to use the Divide and Conquer approach and sorting within individual "buckets" to
achieve overall sorting.

import java.util.*;

public class BucketSortTest {


public static void main(String[] args) {
// Given array of floating-point numbers
double[] arr = {.79, .13, .16, .64, .39, .20, .89, .53, .71, .42};

// Displaying the original array


System.out.println("Original array: ");
for (double num : arr) {
System.out.print(num + " ");
}
System.out.println();

// Perform Bucket Sort


bucketSort(arr);

// Displaying the sorted array


System.out.println("Sorted array: ");
for (double num : arr) {
System.out.print(num + " ");
}
System.out.println();
}

// Bucket Sort method


public static void bucketSort(double[] arr) {
// Step 1: Create empty buckets (array of ArrayLists)
int n = arr.length;
@SuppressWarnings("unchecked")
ArrayList<Double>[] buckets = new ArrayList[n];

// Step 2: Initialize each bucket


for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<>();
}

// Step 3: Distribute the elements into different buckets


for (double num : arr) {
int bucketIndex = (int) (num * n); // Mapping number to a bucket
buckets[bucketIndex].add(num);
}

// Step 4: Sort each bucket and concatenate


int index = 0;
for (ArrayList<Double> bucket : buckets) {
Collections.sort(bucket); // Sorting individual buckets
for (double num : bucket) {
arr[index++] = num; // Putting the sorted elements back into the original array
}
}
}
}

Expected Output
1. Original array: 0.79 0.13 0.16 0.64 0.39 0.2 0.89 0.53 0.71 0.42

2. Sorted array: 0.13 0.16 0.2 0.39 0.42 0.53 0.64 0.71 0.79 0.89

You might also like