You are on page 1of 8

14/02/2021 Bucket Sort: Visualize, Design, and Analyse.

| by Vikram Gupta | Javarevisited | Jan, 2021 | Medium

You have 2 free member-only stories left this month. Sign up for Medium and get an extra one

Bucket Sort: Visualize, Design, and Analyse.


Know complete analysis of Bucket Sort Algorithm.

Vikram Gupta
Jan 13 · 4 min read

Bucket sort

Let’s talk about the Bucket Sort algorithm: Its visualization, design, and analysis.

What Is Bucket sort?


Bucket Sort is used when input numbers are uniformly distributed over a range. The
following problem illustrates the use of the bucket sort algorithm.

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}.

How can we sort these numbers efficiently?

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.

Let’s Visualize the Bucket Sort Algorithm by Taking An example:


1. Given an array and creating a new array to hold the elements of the given 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.

Similarly, .25 is also inserted into the same bucket.

After inserting all the elements into their respective buckets.

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

Now Let’s Design the Bucket Sort Algorithm :

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.

Java Program for Bucket Sort:

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 }

BucketSort java hosted with ❤ by GitHub view raw


https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 5/8
BucketSort.java hosted withBucket
14/02/2021
❤ bySort:GitHub
Visualize, Design, and Analyse. | by Vikram Gupta | Javarevisited | Jan, 2021 | Medium
view raw
Bucket Sort

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).

Best Case Complexity: O(n+k).


Best case time complexity occurs when the array elements are uniformly
distributed in all the buckets with each bucket containing an equal number of
elements. The best-case time is O(n+k) where O(n) is the complexity for making
the buckets and O(k) is the complexity for sorting the elements of the bucket with
the help of an algorithm that has linear time complexity in the best case.

Is Bucket Sort stable?

Definition of stable : the relative order of elements with the same value is
maintained or preserved.

Bucket sort may or may be stable depending on the


algorithm used to sort the bucket elements.

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

Is Bucket Sort In-place?

It uses extra space for sorting the array elements


(bucket arrays), hence it is not an In-place sorting
algorithm.

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.

Visualizing, Designing, and Analyzing the Radix Sort


Algorithm.
Know complete analysis of Radix Sort Algorithm.
levelup.gitconnected.com

Visualizing, Designing, and Analyzing the Quick Sort


Algorithm.
Know the most asked sorting data structure interview question.
levelup.gitconnected.com

Visualizing, Designing, and Analyzing the Heap Sort


Algorithm.
Complete analysis of HeapSort Algorithm.
levelup.gitconnected.com

Reference: Introduction to algorithms.

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

Data Structures Algorithms Coding Java Programming

About Help Legal

Get the Medium app

https://medium.com/javarevisited/bucket-sort-visualize-design-and-analyse-6b611834630 8/8

You might also like