Overview & Concept
Bucket Sort distributes elements into a set of uniformly partitioned "buckets". Each bucket is then sorted individually using Insertion Sort or recursively, and the contents of the buckets are concatenated in order. It achieves linear average-case O(n + k) time for uniformly distributed data.
How It Works
Range Partitioning: Divides the value interval into k uniform sub-intervals (buckets).
Scatter: Distributes each array element into its corresponding bucket based on index = ⌊n × A[i]⌋.
Individual Bucket Sorting: Sorts each bucket using an efficient stable algorithm like Insertion Sort.
Gather: Concatenates all non-empty buckets sequentially to form the sorted result.
Step-by-Step Execution
Asymptotic Complexity Analysis
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Complexity Bound | O(n + k) | O(n + k) | O(n²) | O(n + k) |
| Explanation | Optimal input arrangement minimizing operations. | Expected time for uniformly distributed randomized inputs. | Adversarial degenerate conditions requiring full traversal. | Auxiliary memory allocated beyond original input size. |
Pseudocode & Invariants
procedure bucketSort(A):
find min and max; create k empty buckets
for each x in A:
compute bucketIndex = floor((x - min) / range * (k - 1))
insert x into bucket[bucketIndex]
for each bucket:
sort bucket individually using insertion sort
concatenate all buckets sequentially back into A
return A
end procedure- Bucket Separation Invariant: All elements in bucket i are strictly less than all elements in bucket i + 1.
Advantages & Disadvantages
Key Advantages
- •Linear average time O(n + k) for uniformly distributed data.
- •Supports floating-point numbers seamlessly.
Trade-offs & Limitations
- •Degenerates to O(n²) if data is clustered into a single bucket (poor distribution).
- •Requires auxiliary memory for bucket collections.
Real-World Applications
Uniform Floating-Point Sorting
Sorting floating-point numbers distributed uniformly in the range [0, 1).
Geographic Location Binning
Clustering GPS coordinates into spatial grid cells.
Decision Guide: When to Use
Ideal Scenarios
- ✓When input numbers are floating-point values known to be uniformly distributed.
Consider Alternatives When
- ✕When data has severe clustering or high variance.
Explore Related Algorithms
Compare trade-offs and complexity across complementary tools in AlgoViz.