# Bucket Sort

Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the following problem.

*Sort a large set of floating point numbers which are in range from 0.0 to 1.0 and are uniformly distributed across the range. How do we sort the numbers efficiently?*

A simple way is to apply a comparison based sorting algorithm. The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is Ω(n Log n), i.e., they cannot do better than nLogn.

Can we sort the array in linear time? Counting sort can not be applied here as we use keys as index in counting sort. Here keys are floating point numbers.

The idea is to use bucket sort. Following is bucket algorithm.

bucketSort(arr[], n)1) Create n empty buckets (Or lists). 2) Do following for every array element arr[i]. .......a) Insert arr[i] into bucket[n*array[i]] 3) Sort individual buckets using insertion sort. 4) Concatenate all sorted buckets.

**Time Complexity:** If we assume that insertion in a bucket takes O(1) time then steps 1 and 2 of the above algorithm clearly take O(n) time. The O(1) is easily possible if we use a linked list to represent a bucket (In the following code, C++ vector is used for simplicity). Step 4 also takes O(n) time as there will be n items in all buckets.

The main step to analyze is step 3. This step also takes O(n) time on average if all numbers are uniformly distributed (please refer CLRS book for more details)

Following is the implementation of the above algorithm.

## C/C++

`// C++ program to sort an array using bucket sort ` `#include <iostream> ` `#include <algorithm> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `// Function to sort arr[] of size n using bucket sort ` `void` `bucketSort(` `float` `arr[], ` `int` `n) ` `{ ` ` ` `// 1) Create n empty buckets ` ` ` `vector<` `float` `> b[n]; ` ` ` ` ` `// 2) Put array elements in different buckets ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `{ ` ` ` `int` `bi = n*arr[i]; ` `// Index in bucket ` ` ` `b[bi].push_back(arr[i]); ` ` ` `} ` ` ` ` ` `// 3) Sort individual buckets ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `sort(b[i].begin(), b[i].end()); ` ` ` ` ` `// 4) Concatenate all buckets into arr[] ` ` ` `int` `index = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = 0; j < b[i].size(); j++) ` ` ` `arr[index++] = b[i][j]; ` `} ` ` ` `/* Driver program to test above funtion */` `int` `main() ` `{ ` ` ` `float` `arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `bucketSort(arr, n); ` ` ` ` ` `cout << ` `"Sorted array is \n"` `; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to sort an array ` `# using bucket sort ` `def` `insertionSort(b): ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(b)): ` ` ` `up ` `=` `b[i] ` ` ` `j ` `=` `i ` `-` `1` ` ` `while` `j >` `=` `0` `and` `b[j] > up: ` ` ` `b[j ` `+` `1` `] ` `=` `b[j] ` ` ` `j ` `-` `=` `1` ` ` `b[j ` `+` `1` `] ` `=` `up ` ` ` `return` `b ` ` ` `def` `bucketSort(x): ` ` ` `arr ` `=` `[] ` ` ` `slot_num ` `=` `10` `# 10 means 10 slots, each ` ` ` `# slot's size is 0.1 ` ` ` `for` `i ` `in` `range` `(slot_num): ` ` ` `arr.append([]) ` ` ` ` ` `# Put array elements in different buckets ` ` ` `for` `j ` `in` `x: ` ` ` `index_b ` `=` `int` `(slot_num ` `*` `j) ` ` ` `arr[index_b].append(j) ` ` ` ` ` `# Sort individual buckets ` ` ` `for` `i ` `in` `range` `(slot_num): ` ` ` `arr[i] ` `=` `insertionSort(arr[i]) ` ` ` ` ` `# concatenate the result ` ` ` `k ` `=` `0` ` ` `for` `i ` `in` `range` `(slot_num): ` ` ` `for` `j ` `in` `range` `(` `len` `(arr[i])): ` ` ` `x[k] ` `=` `arr[i][j] ` ` ` `k ` `+` `=` `1` ` ` `return` `x ` ` ` `# Driver Code ` `x ` `=` `[` `0.897` `, ` `0.565` `, ` `0.656` `, ` ` ` `0.1234` `, ` `0.665` `, ` `0.3434` `] ` `print` `(` `"Sorted Array is"` `) ` `print` `(bucketSort(x)) ` ` ` `# This code is contributed by ` `# Oneil Hsiao ` |

*chevron_right*

*filter_none*

**Output:**

Sorted array is 0.1234 0.3434 0.565 0.656 0.665 0.897

**Bucket Sort To Sort an Array with Negative Numbers**

**References:**

Introduction to Algorithms 3rd Edition by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

http://en.wikipedia.org/wiki/Bucket_sort

**Snapshots:**

## Quiz on Bucket Sort

**Other Sorting Algorithms on GeeksforGeeks/GeeksQuiz:**

- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Heap Sort
- QuickSort
- Radix Sort
- Counting Sort
- ShellSort

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Sort elements by frequency | Set 1
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Merge Sort for Linked Lists
- Sort an array of 0s, 1s and 2s
- std::sort() in C++ STL
- Sort a nearly sorted (or K sorted) array
- Sort numbers stored on different machines
- Iterative Quick Sort
- Sort a linked list of 0s, 1s and 2s
- Counting Sort
- Sort elements by frequency | Set 2
- Radix Sort
- Sort n numbers in range from 0 to n^2 - 1 in linear time
- Sort an array according to the order defined by another array
- Time complexity of insertion sort when there are O(n) inversions?
- Sort an array in wave form
- Iterative Merge Sort
- Merge Sort for Doubly Linked List
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- How to efficiently sort a big list dates in 20's