Bucket sort is a sorting technique that involves dividing elements into various groups, or buckets. These buckets are formed by uniformly distributing the elements. Once the elements are divided into buckets, they can be sorted using any other sorting algorithm. Finally, the sorted elements are gathered together in an ordered fashion.
Bucket Sort Algorithm:
Create n empty buckets (Or lists) and do the following for every array element arr[i].
- Insert arr[i] into bucket[n*array[i]]
- Sort individual buckets using insertion sort.
- Concatenate all sorted buckets.
How does Bucket Sort work?
To apply bucket sort on the input array [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68], we follow these steps:
Step 1: Create an array of size 10, where each slot represents a bucket.

Creating Buckets for sorting
Step 2: Insert elements into the buckets from the input array based on their range.
Inserting elements into the buckets:
- Take each element from the input array.
- Multiply the element by the size of the bucket array (10 in this case). For example, for element 0.23, we get 0.23 * 10 = 2.3.
- Convert the result to an integer, which gives us the bucket index. In this case, 2.3 is converted to the integer 2.
- Insert the element into the bucket corresponding to the calculated index.
- Repeat these steps for all elements in the input array.

Inserting Array elements into respective buckets
Step 3: Sort the elements within each bucket. In this example, we use quicksort (or any stable sorting algorithm) to sort the elements within each bucket.
Sorting the elements within each bucket:
- Apply a stable sorting algorithm (e.g., quicksort) to sort the elements within each bucket.
- The elements within each bucket are now sorted.

Sorting individual bucket
Step 4: Gather the elements from each bucket and put them back into the original array.
Gathering elements from each bucket:
- Iterate through each bucket in order.
- Insert each individual element from the bucket into the original array.
- Once an element is copied, it is removed from the bucket.
- Repeat this process for all buckets until all elements have been gathered.

Inserting buckets in ascending order into the resultant array
Step 5: The original array now contains the sorted elements.
The final sorted array using bucket sort for the given input is [0.12, 0.17, 0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78, 0.94].

Return the Sorted Array
Implementation of Bucket Sort Algorithm
Below is the implementation for the Bucket Sort:
C++14
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void bucketSort( float arr[], int n)
{
vector< float > b[n];
for ( int i = 0; i < n; i++) {
int bi = n * arr[i];
b[bi].push_back(arr[i]);
}
for ( int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());
int index = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}
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;
}
|
Java
import java.util.*;
import java.util.Collections;
class GFG {
static void bucketSort( float arr[], int n)
{
if (n <= 0 )
return ;
@SuppressWarnings ( "unchecked" )
Vector<Float>[] buckets = new Vector[n];
for ( int i = 0 ; i < n; i++) {
buckets[i] = new Vector<Float>();
}
for ( int i = 0 ; i < n; i++) {
float idx = arr[i] * n;
buckets[( int )idx].add(arr[i]);
}
for ( int i = 0 ; i < n; i++) {
Collections.sort(buckets[i]);
}
int index = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < buckets[i].size(); j++) {
arr[index++] = buckets[i].get(j);
}
}
}
public static void main(String args[])
{
float arr[] = { ( float ) 0.897 , ( float ) 0.565 ,
( float ) 0.656 , ( float ) 0.1234 ,
( float ) 0.665 , ( float ) 0.3434 };
int n = arr.length;
bucketSort(arr, n);
System.out.println( "Sorted array is " );
for ( float el : arr) {
System.out.print(el + " " );
}
}
}
|
Python3
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
for i in range (slot_num):
arr.append([])
for j in x:
index_b = int (slot_num * j)
arr[index_b].append(j)
for i in range (slot_num):
arr[i] = insertionSort(arr[i])
k = 0
for i in range (slot_num):
for j in range ( len (arr[i])):
x[k] = arr[i][j]
k + = 1
return x
x = [ 0.897 , 0.565 , 0.656 ,
0.1234 , 0.665 , 0.3434 ]
print ( "Sorted Array is" )
print (bucketSort(x))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
static void bucketSort( float [] arr, int n)
{
if (n <= 0)
return ;
List< float >[] buckets = new List< float >[ n ];
for ( int i = 0; i < n; i++) {
buckets[i] = new List< float >();
}
for ( int i = 0; i < n; i++) {
float idx = arr[i] * n;
buckets[( int )idx].Add(arr[i]);
}
for ( int i = 0; i < n; i++) {
buckets[i].Sort();
}
int index = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < buckets[i].Count; j++) {
arr[index++] = buckets[i][j];
}
}
}
public static void Main()
{
float [] arr = { ( float )0.897, ( float )0.565,
( float )0.656, ( float )0.1234,
( float )0.665, ( float )0.3434 };
int n = arr.Length;
bucketSort(arr, n);
Console.WriteLine( "Sorted array is " );
foreach ( float el in arr)
{
Console.Write(el + " " );
}
}
}
|
Javascript
<script>
function bucketSort(arr,n)
{
if (n <= 0)
return ;
let buckets = new Array(n);
for (let i = 0; i < n; i++)
{
buckets[i] = [];
}
for (let i = 0; i < n; i++) {
let idx = arr[i] * n;
let flr = Math.floor(idx);
buckets[flr].push(arr[i]);
}
for (let i = 0; i < n; i++) {
buckets[i].sort( function (a,b){ return a-b;});
}
let index = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < buckets[i].length; j++) {
arr[index++] = buckets[i][j];
}
}
}
let arr = [0.897, 0.565,
0.656, 0.1234,
0.665, 0.3434];
let n = arr.length;
bucketSort(arr, n);
document.write( "Sorted array is <br>" );
for (let el of arr.values()) {
document.write(el + " " );
}
</script>
|
Output
Sorted array is
0.1234 0.3434 0.565 0.656 0.665 0.897
Complexity Analysis of Bucket Sort Algorithm
Time Complexity: O(n2),
- 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.
- 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.
Auxiliary Space: O(n+k)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!