Short answer: Bucket sort drops keys from a known numeric range into n buckets, sorts each bucket, and concatenates them. When keys are independent and uniform, the buckets stay small and the average time is Θ(n). One bucket that receives every key falls back to the inner sort, which is Θ(n²) if that inner sort is insertion sort. The uniform spread is a hypothesis about the input, not a property the algorithm enforces.
What the problem actually asks
Keys are real numbers in a half-open interval, conventionally [0, 1), or integers that have been scaled into that interval. A key x belongs in bucket floor(n·x). Inside a bucket the keys occupy a short range and can be sorted by insertion sort. Concatenating bucket 0 through bucket n−1 preserves global order because every key in an earlier bucket is smaller than every key in a later bucket.
The average-case proof needs the keys to be drawn independently from a uniform distribution, so that the bucket sizes are balanced with high probability. A distribution that clumps breaks the balance and leaves the proof behind.
What the algorithm guarantees
Scattering is Θ(n), and concatenating is Θ(n). The inner sorts together cost time proportional to the sum of squares of the bucket sizes. Under independent uniform keys that sum is Θ(n) in expectation, so the expected running time is Θ(n). The worst case, every key in one bucket, is the worst case of the inner sort. Extra memory is Θ(n) for the buckets.
The sort is stable when each bucket’s inner sort is stable and records are appended to a bucket in input order. Insertion sort meets that condition. The interval must be known. A key of 1.0 in a [0, 1) layout is out of range and has to be placed by an explicit rule, usually a last bucket.
The working steps
- Scale the keys into [0, 1), using the known minimum and maximum when the raw range is something else. Reject or clamp a key that falls outside after scaling, under an explicit rule.
- Allocate n empty buckets. Send key x to bucket floor(n·x), and to bucket n−1 if a scaled key would otherwise land on n.
- Sort each bucket with insertion sort.
- Write the buckets out in index order.
- If the input distribution is known to be non-uniform, change the bucket boundaries to equal-probability quantiles. Equal-width buckets are the uniform case only.
Expected linear time is a statement about uniform independent keys and an inner sort whose cost is quadratic in the bucket size. Integers in a small discrete range are counting sort. Fixed-width words are radix sort. Neither needs a uniformity hypothesis.
Approaches that look simpler and fail
| Approach | Why it fails when the uniform-bucket hypothesis is false |
|---|---|
| Equal-width buckets on a skewed distribution | A clump falls in one bucket. Expected linear time was a statement about uniform draws, and this input is the worst case of insertion sort. |
| Skip the inner sort because the buckets look ordered | A bucket holds an unordered subset of a narrow range. Order inside the bucket still has to be computed. |
| Use n² buckets to force tiny lists | Memory and the scatter pass then dominate, and the algorithm has become a sparse counting sort with a worse constant. |
| Apply it to keys that only support comparison | The bucket index is an arithmetic function of the key and the range. A comparison oracle does not provide one. |
Bounded integer keys belong to counting sort and radix sort, which have worst-case linear bounds in n plus the digit parameters. A comparison sort is the fallback when no range and no distribution are known.
A working example
This Python fragment scatters values in [0, 1) into equal buckets, sorts each bucket, and concatenates them. It prints 0.12 0.17 0.21 0.35 0.78.
values = [0.78, 0.17, 0.21, 0.35, 0.12]
n = len(values)
buckets = [[] for _ in range(n)]
for v in values:
buckets[min(n - 1, int(v * n))].append(v)
for bucket in buckets:
bucket.sort()
out = [v for bucket in buckets for v in bucket]
assert out == sorted(values)
print(*(f"{v:.2f}" for v in out))
Where it is used
Bucket sort is the distribution sort for keys that are spread evenly, such as normalized scores or uniform random floats. Uneven keys dump into one bucket and the method falls back to whatever sorts that bucket. Integers in a small range use counting sort instead.
When you can drop this algorithm
Drop bucket sort when the distribution may clump, unless the bucket boundaries are rebuilt from real quantiles and that cost is included. Use radix sort for machine words and mergesort or heapsort for general keys. Keep bucket sort for numeric keys that really are spread evenly across a known interval.
Why this stays published
We publish this so a search for bucket sort returns the uniform hypothesis that the expected linear bound depends on. Bucket sort expects linear time when keys are spread uniformly across n buckets, and it degrades to the inner sort when they are not.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
