Bucket Sort in Data Structure
Bucket Sort works by distributing elements of an array into a number of 'buckets', sorting each bucket individually (often using Insertion Sort), and then concatenating the sorted buckets to produce the final sorted array.
Algorithm Steps
- Create a fixed number of empty buckets.
- Distribute each input element into a bucket based on its value (commonly by scaling to the number of buckets).
- Sort each individual bucket, typically using Insertion Sort.
- Concatenate all buckets in order to get the final sorted array.
def bucket_sort(arr):
if not arr:
return arr
num_buckets = len(arr)
max_val, min_val = max(arr), min(arr)
range_val = (max_val - min_val) / num_buckets + 1e-9
buckets = [[] for _ in range(num_buckets)]
for num in arr:
idx = int((num - min_val) / range_val)
buckets[idx].append(num)
result = []
for bucket in buckets:
bucket.sort() # insertion sort could be used here instead
result.extend(bucket)
return result
Complexity
| Case | Time |
|---|---|
| Best/Average (uniformly distributed data) | O(n + k) |
| Worst (all elements in one bucket) | O(n²) |
Applications
- Sorting uniformly distributed floating-point numbers (e.g., values between 0 and 1).
- External sorting when data can be partitioned into ranges.
- Used in histogram-based data processing.
Choosing the Bucket Count and Range
Bucket sort only shines when the input is spread fairly evenly across its range. The index formula int((num - min_val) / range_val) maps each value to a bucket, and the quality of that mapping decides everything. If the data clusters in one region, most values land in a single bucket, the inner sort runs on a large list, and performance slides toward O(n squared). For uniformly spread data the buckets stay small and the total work is close to linear.
A common choice is one bucket per element, as in the sample code. Fewer buckets save memory but make each inner sort heavier; more buckets do the reverse. There is no single right number — it depends on how your values are distributed.
Walking Through a Sample Distribution
Sort [0.78, 0.17, 0.39, 0.26, 0.72] with five buckets covering 0 to 1. Value 0.17 maps to bucket 0, 0.26 to bucket 1, 0.39 to bucket 1, 0.72 to bucket 3, 0.78 to bucket 3. Bucket 1 holds [0.26, 0.39] and bucket 3 holds [0.72, 0.78]; a quick insertion sort fixes any out-of-order pairs. Concatenating buckets in order gives [0.17, 0.26, 0.39, 0.72, 0.78]. Because each bucket held only one or two items, almost no comparison work was needed.
When Bucket Sort Beats Comparison Sorts
Comparison sorts cannot go below O(n log n) in the general case. Bucket sort escapes that bound by not comparing most pairs at all — it places values by arithmetic instead. That makes it a strong fit for:
- Uniformly distributed floating-point numbers, such as sensor readings normalised to a range.
- Data that naturally splits into ranges, like scores grouped into bands.
- External sorting where chunks can be bucketed before processing.
Avoid it for skewed data, unknown ranges, or when stability within a bucket matters and your inner sort is not stable. The biggest real-world mistake is assuming uniformity that is not there — always sample the data first.
Takeaway: bucket sort trades generality for speed on evenly spread values. Pick a bucket scheme that keeps each bucket small, and the whole array sorts in roughly linear time.