It's a sorting algorithm that divides elements into a number of groups called buckets. Elements are distributed among these buckets based on their values, each bucket is sorted separately, and finally all buckets are combined to form the sorted array.
Working
Consider the following array: [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68].
Step 1: Create Empty Buckets
- Each bucket represents a portion of the input range.
- Buckets are indexed from 0 to 9.
- No elements are stored initially.

Step 2: Distribute Elements into Buckets
- Values between 0 and 1 can be calculated as: [ index = int(value * number_of_buckets)]
- Each element is placed into the bucket corresponding to its value.

Step 3: Sort Elements Withing Each Bucket
- Each bucket is handled independently.
- Empty buckets are skipped.
- Insertion Sort can be used to sort the elements within a bucket.
- Smaller bucket sizes reduce the work required for sorting.

Step 4 : Combine the sorted Buckets
- Elements from Bucket 0 are collected first.
- Buckets are then processed sequentially.
- Sorted elements from each bucket are appended to the result.
- Ordering buckets by index ensures smaller values appear before larger values.

Step 5: Obtain the Sorted Array
- Every element has been processed.
- Values inside each bucket are sorted.
- Combining buckets in order produces the final sorted result.

Implementation
Below is the implementation of bucket sort in python:
def insertion_sort(bucket):
for i in range(1, len(bucket)):
key = bucket[i]
j = i - 1
while j >= 0 and bucket[j] > key:
bucket[j + 1] = bucket[j]
j -= 1
bucket[j + 1] = key
def bucket_sort(arr):
n = len(arr)
# Create empty buckets
buckets = [[] for _ in range(n)]
# Distribute elements into buckets
for num in arr:
bucket_index = int(num * n)
buckets[bucket_index].append(num)
# Sort individual buckets
for bucket in buckets:
insertion_sort(bucket)
# Merge all buckets
index = 0
for bucket in buckets:
for num in bucket:
arr[index] = num
index += 1
arr = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]
bucket_sort(arr)
print("Sorted array:")
print(arr)
bucket_sort(arr)
print("Sorted array is:")
print(" ".join(map(str, arr)))
Output
Sorted array: [0.12, 0.17, 0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78, 0.94] Sorted array is: 0.12 0.17 0.21 0.23 0.26 0.39 0.68 0.72 0.78 0.94
Explanation:
- buckets create n empty lists.
- bucket_index decides where each value should be stored.
- insertion_sort() sorts every individual bucket.
- Final nested loops copy sorted elements back into arr.
- Given implementation assumes values are in the range (0,1).
Complexity Analysis
Let n be the number of elements and k be the number of buckets.
- Distribution of n elements requires O(n) time.
- Traversing all k buckets requires O(k) time.
- Under uniform distribution, sorting work inside buckets remains small, giving an average complexity close on O(n+k).
- When most elements fall into one bucket, insertion sort can require O(n2) time.
- Buckets require O(n+k) auxiliary space.
Advantages
- Works efficiently for uniformly distributed data.
- Can achieve linear-time performance for suitable input distributions.
- Individual buckets can be sorted independently.
- Different sorting techniques can be used for sorting buckets.
- Useful for numerical values belonging to a known range.
Disadvantages
- Performance depends heavily on input distribution.
- Poor distribution can create large buckets and increase sorting time.
- Requires additional memory for storing buckets.
- Bucket-index calculation must be chosen according to the input range.
- Not suitable when values are distributed unpredictably.