Professional Documents
Culture Documents
Explanation:
Step 1:
Since we have the array with integer elements, we'll calculate the range first.
Step 2: Scatter
Now, iterate through the unsorted array and keep inserting the numbers in the
bucket of their corresponding range.
Step 3:
Now sort all the elements in each of the buckets, and the sorted buckets look like
this:
Step 4: Gather
At last, visit each bucket and gather all the numbers together. Merge them all, and
we'll get the sorted array.
Program Code:
import java.util.*;
import java.util.Collections;
class bucket {
// Driver code
public static void main(String args[])
{
float array[] = { (float)0.42, (float)0.32,
(float)0.35, (float)0.52,
(float)0.39, (float)0.47,
(float)0.50};
int n = array.length;
System.out.print("Before sorting array elements are - \n");
printArr(array, n);
bucketSorting(array, n);
System.out.println("");
System.out.println("Sorted Array is: ");
for (float element : array) {
System.out.print(element + " ");
}
}
}
Output Screenshots:
correct input
RESULT: Implementation of Bucket Sort Thus, the programs for the given problem statements has been
executed and the results are verified successfully.