Bucket Sort

Distribution sort: scatter elements into k buckets, sort each bucket independently, then gather buckets in order. O(n) average on uniform input; O(n^2) worst on skewed input.

Best: O(n + k) Avg: O(n) Worst: O(n^2) Space: O(n + k) Stable if inner sort is stable
slow fast
Bucket 0
Bucket 1
Bucket 2
Bucket 3
Bucket 4
Comparing
Sorted
Step
0/0
Phase
-
Buckets (k)
0
Comparisons
0
Max bucket
0
Input array
Buckets (after scatter, sorted within)
Output (gather)

Algorithm Explanation

Press Play to animate, or Step for single-step. Try Skewed to see Bucket Sort's worst case in action.
OperationBestAverageWorstNote
ScatterO(n)O(n)O(n)One pass
Inner sort (per bucket)O(1)O(1)O(n^2)Insertion sort
GatherO(n + k)O(n + k)O(n + k)Concatenation
Total timeO(n + k)O(n)O(n^2)O(n) when k ≈ n, uniform
SpaceO(n + k)O(n + k)O(n + k)Not in-place