Definition · The two pivot rules, and what a partition costsQuicksort on a block of length L chooses a pivot, partitions the block against it at a cost of L − 1 comparisons, and recurses on the two pieces. The fixed rule always takes the last element of the block. The random rule takes a uniformly random position in the block, independently at every call. The comparison count C is the total over all partitions, and it is the only cost this lesson measures.
On a sorted array the fixed rule pays the worst case. Every pivot is the largest remaining key, so every partition splits a block of length L into one of length L − 1 and one of length 0, and the total is 7 + 6 + 5 + 4 + 3 + 2 + 1 = 28, which is n(n − 1)/2. It is 28 on the reversed array too, for the mirror-image reason. On the alternating array it is 16. One rule, three inputs, three different counts — and every one of them is that rule's cost on that input for ever.
Definition · The distribution of the random rule's costFor each n, D(n) is the probability mass function of the comparison count when every pivot is uniformly random. D(0) and D(1) are the point mass at 0. For k ≥ 2, the first partition costs k − 1, and the pivot's rank is uniform on 1..k, leaving blocks of sizes i and k − 1 − i whose costs are independent. So D(k) is k − 1 plus the average over i of the convolution of D(i) and D(k − 1 − i).
That recursion is over sizes rather than over tapes, which is what makes it cheap: the cost of an execution depends only on how the pivots split the blocks, not on which elements they were. At n = 8 it produces sixteen probabilities, one for each attainable count from 13 to 28, and they add to 1 — a check the panel performs and prints rather than assuming.
Example · The distribution at n = 8, in fullP(C = 13) = 1/24, P(14) = 13/72, P(15) = 3/20, P(16) = 73/360, P(17) = 29/840, P(18) = 59/420, P(19) = 31/420, P(20) = 5/84, P(21) = 1/36, P(22) = 11/252, P(23) = 17/1260, P(24) = 1/63, P(25) = 2/315, P(26) = 1/210, P(27) = 1/630 and P(28) = 1/315. The most likely count is 16 and the mean is 2369/140 ≈ 16.92, so the two are not the same number; and the shape is not a smooth hump — P(17) is a sixth of P(16) and a quarter of P(18).
That jaggedness is worth a second look, because it is the first thing a reader expecting a bell curve will try to explain away. It is real. A comparison count is a sum over partitions of L − 1, and the attainable sums at small n are constrained by arithmetic rather than by anything smooth: some totals have many ways to arise and some have few. Nothing about the expectation requires the shape around it to be well behaved, which is exactly why the next lesson's bounds are stated for every distribution and not for nice ones.
Theorem · The closed formFor n ≥ 1, the expected number of comparisons made by randomised quicksort on n distinct keys is 2(n + 1)Hₙ − 4n, where Hₙ is the n-th harmonic number. It does not depend on the input.
The derivation is Sorting and Selection's, by indicator variables: the i-th and j-th smallest keys are compared exactly when one of them is the first pivot drawn from the stretch of sorted order between them, which has probability 2/(j − i + 1), and linearity adds the indicators up. This course uses it as an oracle rather than re-deriving it. H₈ = 761/280, so the closed form gives 2·9·761/280 − 32 = 2369/140, and the panel prints it beside the recursion's answer with the word agrees or the word DISAGREES.
A third route, and the check a comparison count cannot perform
At n ≤ 7 the lab does something different again: it runs the fixed-pivot algorithm on every one of the n! input orders and tallies what it counted. At n = 5 that is 120 runs, and the resulting distribution agrees with the recursion value by value, with an expectation of 37/5 from both. The agreement is a theorem, not a coincidence: randomising the pivot on a fixed input and fixing the pivot on a uniformly random input are the same experiment.
That pass also checks something no comparison count can. Every one of those 120 runs is required to have returned a sorted array that is a permutation of its input, and the seeded runs are checked the same way. An unsorted array has a comparison count too, and a sorting arm whose only assertion is a count will report a confident number about a broken sort. The panel's last figure reads yes, all 120 for exactly that reason.
The measured column, and what moving a slider proves
Over seeds 1 to 120 on the sorted array the mean comes out 415/24, which is 17.2917, against an expectation of 16.9214: high by 311/840. On the reversed array the same 120 seeds give 1031/60, and on the alternating array 1003/60, which is below the expectation. The exact distribution is identical in all three cases. Three different measured means, one expectation, and the only thing that changed was an input the expectation does not depend on.
The observed range is the other half of the story: 13 to 27 over those 120 seeds on the sorted array, 13 to 26 on the reversed one, 13 to 28 on the alternating one. The support of the true distribution is 13 to 28 in every case. A measured range is a subset of the support and it is a different object from it — the 120 seeds that never produced 28 do not make 28 impossible, and P(C = 28) = 1/315 says how impossible it is not.
The one claim here that holds always
C ≤ n(n − 1)/2 on every execution, because each partition compares the pivot with each other element of its block at most once and each pair of elements ends up in the same block for at most one partition. The panel checks that the support of the exact distribution sits inside that bound rather than quoting it. That is the only always claim on this page; the expectation is not one, and neither is the mean.