Definition · Randomised quicksortRandomised quicksort is quicksort with one change: the pivot of each call is chosen uniformly at random from that call's range. The input is not shuffled and nothing is assumed about it. Every run on the same array may cost a different number of comparisons.
Rename the keys by rank: z₁ is the smallest, zₙ the largest. This is a relabelling, not an assumption — the algorithm never sees the names. Let Xᵢⱼ be 1 if zᵢ and zⱼ are compared at some point during the run and 0 otherwise, and let X be the total number of comparisons, which is the sum of the Xᵢⱼ over all i < j.
Theorem · The comparison criterionFor i < j, the keys zᵢ and zⱼ are compared during the run if and only if the first pivot chosen from among zᵢ, zᵢ₊₁, …, zⱼ is zᵢ or zⱼ. Consequently P(Xᵢⱼ = 1) = 2/(j − i + 1).
All of zᵢ … zⱼ begin in the same call, since every call holds a contiguous stretch of sorted order and this stretch has not been split yet. Consider the first pivot drawn from that stretch.
If it is some z_m with i < m < j, then zᵢ goes to the left side and zⱼ to the right, they are never in the same call again, and they are never compared: a comparison only ever happens between a pivot and another element of the same call. If it is zᵢ or zⱼ, that one is the pivot of a call containing the other, so the two are compared exactly then.
Every element of the stretch is equally likely to be the first of the stretch drawn, because each call draws uniformly from its range. The stretch has j − i + 1 elements and two of them give a comparison, so the probability is 2/(j − i + 1).
□
E[X] = Σ Σ 2/(j − i + 1) the double sum over i < j
i=1..n−1 j=i+1..n
= Σ Σ 2/(d + 1) d = j − i
i=1..n−1 d=1..n−i
= 2(n+1)Hₙ − 4n Hₙ = 1 + 1/2 + … + 1/n
< 2n ln n
The inner sum is a tail of the harmonic series, and collecting the identical terms across the outer sum gives the closed form. The bound Hₙ < 1 + ln n turns it into 2n ln n, which is the form usually quoted — but the closed form is exact and the lab prints it as a fraction, because Hₙ is computed as a rational and not as a float.
Example · Nine keys, three waysAt n = 9 the closed form is 2593/126 = 20.5794 comparisons, with H₉ = 7129/2520. The lab computes the same number twice more. Once by solving T(n) = (n−1) + (1/n)Σ(T(i) + T(n−1−i)) from the bottom, which contains no harmonic number at all. And once by enumerating every one of the 4 862 possible pivot sequences on the array on screen and averaging exactly. Three definitions, one fraction.
That third row is the one that matters, because it is not a sample. It is a claim about every run of the algorithm on that array, computed rather than argued. Run it on a sorted array, a reversed array, an up-then-down array, a seeded shuffle and the median-of-three killer, and all five give 2593/126. The expectation does not know which array it was handed, which is exactly what the derivation said.
Beside those exact rows the lab prints a mean over seeds, and that one is a sample. 120 seeds on the killer array at nine keys average 20.6583, which is 0.0790 above the expectation. Move the first seed and the row moves; nothing above it does. The gap is sampling, and the honest description of it is “120 of the 4 862 runs”.
What the expectation assumes, and the array that breaks it
The derivation used distinct keys twice: once in the relabelling and once in the claim that a comparison between a pivot and an element sends the element to one side or the other. On an array of 24 equal keys, Lomuto sends every key to the same side, the split is 0 and n − 1 under every seed, and the cost is 276 comparisons while 2(n+1)Hₙ − 4n at 24 is 92.7979. The formula has not been beaten; it was never a claim about that array. Sorting with many equal keys wants a three-way partition, which this course does not cover.
This is the pattern the rest of the path follows. An expectation over the algorithm's own coins holds for every input; an expectation over a distribution of inputs holds only while the distribution does. “Bucket Sort and Average-Case Claims” is the second kind, put beside this one on purpose.