Bucket Sort
Overview
Bucket sort distributes inputs into buckets according to their key range, sorts each bucket individually (typically with insertion sort), and concatenates the buckets in order. It assumes the input is drawn uniformly from a known interval. Under this assumption, each bucket receives approximately elements and the average-case running time is .
Worst-case running time is if all elements fall into one bucket, since insertion sort on elements is .
Algorithm
For input in the range :
- Create empty buckets (linked lists or arrays).
- For each element , insert it into bucket .
- Sort each bucket (e.g. with insertion sort).
- Concatenate buckets in order (0, 1, …, ).
The bucket index maps the uniform range onto equal-width intervals.
Worked Example
Input: , .
Step 2: Distribute into buckets ():
| Bucket | Range | Elements |
|---|---|---|
| 0 | — | |
| 1 | 0.17, 0.12 | |
| 2 | 0.26, 0.21, 0.23 | |
| 3 | 0.39 | |
| 4 | — | |
| 5 | — | |
| 6 | 0.68 | |
| 7 | 0.78, 0.72 | |
| 8 | — | |
| 9 | 0.94 |
Step 3: Sort each bucket (insertion sort):
- Bucket 1:
- Bucket 2:
- Bucket 3:
- Bucket 6:
- Bucket 7:
- Bucket 9:
Step 4: Concatenate:
Complexity Analysis
Distribution into buckets: . Sorting each bucket: let be the number of elements in bucket . Insertion sort on bucket costs . Total:
Under the uniform distribution assumption, the expected value of is , giving .
If the uniformity assumption fails and all elements land in one bucket, insertion sort on that bucket costs , which is the worst case.
Comparison with Counting Sort and Radix Sort
| Sort | Assumption | Average | Worst |
|---|---|---|---|
| Counting sort | Integer keys in | ||
| Radix sort | -digit integers | ||
| Bucket sort | Uniform real keys in |
Bucket sort is the only one of the three whose worst case is quadratic. Its average-case speed depends on the uniform-distribution assumption, whereas counting and radix sort guarantee their bounds for all inputs satisfying their range constraints.
Practical Use
Bucket sort is most useful when input is known to be uniformly distributed — for example, hashing a uniform key space into buckets. In practice, it is often combined with other techniques (e.g. using a better sort within buckets, or using it as one phase of a hybrid algorithm).
Summary
| Property | Value |
|---|---|
| Type | Non-comparison, distribution sort |
| Average time | |
| Worst-case time | |
| Space | for buckets |
| Stable | Depends on bucket sort |
| Assumption | Uniform distribution of keys |
| Beats | Yes on average (not comparison-based) |