Definition · Bucket sortBucket sort over a key range [0, U) with b buckets: place each key x into bucket ⌊bx/U⌋, sort each bucket with insertion sort, and concatenate the buckets in order. The buckets are equal in width; nothing makes them equal in occupancy.
The scatter is Θ(n) and the concatenation is Θ(n + b). Everything else is the insertion sorts, and their cost depends on the bucket sizes, which depend on the keys. So the analysis is not about the algorithm at all: it is about a random variable determined by where the keys fall.
if every bucket holds n/b keys, insertion sort inside one costs
(n/b)(n/b − 1)/2 at worst
summed over b buckets:
b · (n/b)(n/b − 1)/2 = n(n/b − 1)/2
n = 48, b = 12: 48 · (4 − 1)/2 = 72 exactly
n = 96, b = 12: 96 · (8 − 1)/2 = 336
n = 48, b = 48: 48 · (1 − 1)/2 = 0
With b = n the expected inner work is zero, which is the sense in which bucket sort is called linear: the remaining cost is the scatter and the concatenation. The lab evaluates the expression as an exact rational, because the point of printing it beside a measurement is that the two are different kinds of number and can be subtracted.
Example · Forty-eight keys, three distributions, one algorithmTwelve buckets throughout. A uniform draw fills them 4 4 9 2 3 0 3 4 3 1 8 7 and costs 90 comparisons against an expectation of 72. Keys clustered in a twentieth of the range put all 48 into one bucket and cost 592. Keys confined to the first bucket cost 582. All three outputs are sorted; only the cost moved, and the code did not.
The gap between 72 and 90 on the uniform draw is worth a sentence of its own, because it is the honest half of the lesson. 72 is an expectation over the distribution; 90 is one draw from it. The bucket sizes above are visibly uneven — one bucket empty, one holding nine — and that unevenness is what a uniform draw looks like at n = 48. A measurement above the expectation is not a refutation of it.
Two expectations, side by side
Put the two claims of this course in the same sentence and the difference is unmissable. Randomised quicksort: for every array, the average over the algorithm's pivots is 2(n+1)Hₙ − 4n. Bucket sort: for keys drawn uniformly, the average over the draw is n(n/b − 1)/2. The first quantifies over inputs and averages over coins the algorithm controls; the second averages over inputs and controls nothing.
That is why one of them survives a hostile input and the other does not. An adversary who picks the array cannot move randomised quicksort's expectation, because the expectation never mentioned the array. An adversary who picks the keys moves bucket sort's cost to Θ(n²) by putting them all in one bucket, and the lab ships exactly that input.
None of which makes bucket sort a bad algorithm. It makes its bound a conditional one, and the condition is checkable: where do the keys come from, and is the range they are spread over the range the buckets divide? Hash values, uniformly generated identifiers and quantised sensor readings often satisfy it. Prices, word lengths and timestamps of human activity usually do not.
One last discipline. The measured 582 on the one-bucket input is not the worst case; n(n − 1)/2 = 1128 is, and insertion sort only reaches it on a reversed bucket. The measured column is one input's cost and the proved column is about all of them, and this page keeps them apart on purpose.