Professional Documents
Culture Documents
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Vikram Gupta
Jan 13 · 4 min read
Bucket sort
Let’s talk about the Bucket Sort algorithm: Its visualization, design, and analysis.
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 1/8
14/02/2021 Bucket Sort: Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
Eg: Sort a large set of floating-point numbers that are in the range
from 0.0 to 1.0 and are uniformly distributed across the range.
Let’s say the numbers are {0.42,0.32,0.23,0.52,0.25,0.47,0.51}.
We may use any Comparison-based sorting algorithm. But the lower bound for the
Comparison-based sorting algorithm is Ω(n log n) (Merge sort, Heap sort, Quick-sort,
etc). These algorithms cannot do better than Ω(n log n).
We may use Counting sort to sort these elements in linear time but Counting sort can
not be applied here as we use keys as the index in counting sort and in this case, keys
are floating-point numbers.
So we’ll use a new sorting algorithm called Bucket sort. Bucket Sort is a sorting
technique that sorts the elements by first dividing the elements into several groups
called buckets. The elements inside each bucket are sorted using any of the suitable
sorting algorithms or recursively calling the same algorithm.
Several buckets are created. Each bucket is filled with a specific range of elements. The
elements inside the bucket are sorted using any other algorithm. Finally, the elements
of the bucket are gathered to get the sorted array.
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 2/8
14/02/2021 Bucket Sort: Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
Bucket sort
2. Insert elements into the buckets from the given array. The elements are inserted
according to the range of the bucket.
Now let’s have buckets ranging from 0 to 1, 1 to 2, 2 to 3,…… (n-1) to n where n is the
size of the array of buckets. In the above example, it is 10.
Let’s take the input element 0.32. It is multiplied by the size of the new array that is
0.32*10 = 3.2 => converts to 3 as new array indices range from 0–9 only and these
indices are integers. Finally, .32 is inserted into bucket-3.
Bucket sort
3. Now, the elements of each bucket are sorted using any of the stable sorting
algorithms. We have used the quicksort which is an inbuilt function in java.
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 3/8
14/02/2021 Bucket Sort: Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
Bucket sort
4. The elements from each bucket are collected and placed into the original array by
iterating through the buckets.
Bucket sort
bucketSort(arr[], n)
1. Create 'n' empty buckets using some data structure.
2. For each array element arr[i].
2.1 Insert arr[i] into bucket[n*array[i]]
3. Sort individual buckets using insertion sort.
4. Now concatenate all the sorted buckets and create single
array.
1
2 import java.util.ArrayList;
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 4/8
14/02/2021 Bucket Sort: Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
3 import java.util.Collections;
4
5 public class BucketSort {
6 public void bucketSort(float[] inputArr, int n) {
7
8 if (n <= 0)
9 return;
10
11 ArrayList<Float>[] bucket = new ArrayList[n];
12
13 //Create 'n' empty buckets
14 for (int i = 0; i < n; i++)
15 bucket[i] = new ArrayList<Float>();
16
17 //Add the elements into the buckets
18 for (int i = 0; i < n; i++) {
19 int bucketIndex = (int) inputArr[i] * n;
20 bucket[bucketIndex].add(inputArr[i]);
21 }
22
23 //Sort the elements of each bucket one by one
24 for (int i = 0; i < n; i++) {
25 Collections.sort((bucket[i]));
26 }
27
28 //Get back the sorted array
29 int index = 0;
30 for (int i = 0; i < n; i++) {
31 for (int j = 0, size = bucket[i].size(); j < size; j++) {
32 inputArr[index++] = bucket[i].get(j);
33 }
34 }
35 }
36
37 //Main as driver method
38 public static void main(String[] args) {
39 BucketSort b = new BucketSort();
40 float[] arr = { (float) 0.42, (float) 0.32, (float) 0.33, (float) 0.52, (float)
41 (float) 0.51 };
42 b.bucketSort(arr, 7);
43
44 System.out.println("Sorted Array : ");
45 for (float i : arr)
46 System.out.print(i + " ");
47 }
48 }
OUTPUT:
Sorted Array :
0.32 0.33 0.37 0.42 0.47 0.51 0.52
Complexity Analysis:
Worst Case Complexity: O(n2)
The worst case occurs when the most number of elements are placed into a single
bucket. If the insertion sort algorithm is used to sort the elements of the bucket
then time complexity becomes O(n2).
Definition of stable : the relative order of elements with the same value is
maintained or preserved.
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 6/8
14/02/2021 Bucket Sort: Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
That’s all for this article. Thank you for reading this article. I hope, you have
understood the Bucket-sort algorithm and its time complexities. You may read my other
useful articles on sorting data structure.
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 7/8
14/02/2021 Bucket Sort: Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 8/8