Learn
Lesson · Randomised Algorithms · Randomness as a resource

The Cost Is a Distribution

Randomised quicksort on a sorted array of eight keys costs 28 comparisons with a fixed pivot, has an expectation of 2369/140 with a random one, and averaged 415/24 over 120 seeds. Those are three different kinds of number and the lesson is that none of them is either of the others. The exact distribution is computed by recursion over subproblem sizes, checked against the closed form, and checked again against running the algorithm on every input order.

fixed pivot on a sorted array: 28 comparisons, every time E[C] = 2369/140 = 16.9214… over every execution, exactly closed form 2(n+1)Hₙ − 4n a second definition, same number mean over 120 seeds: 415/24 a MEASUREMENT, and it moves support 13 ≤ C ≤ 28 an ALWAYS claim, with 28 = n(n−1)/2 Var(C) = 160599/19600 exact, and the next lesson needs it
One input, one algorithm, and three numbers that are not each other

Core ideas

What this lesson establishes

The hard idea is that the second number is not an average of runs. The other two are what changes when the input moves and what changes when the seeds move.

1

A randomised algorithm has a cost per execution, not a cost

Fix the input and the fixed-pivot rule and there is one number: on a sorted array of eight keys, 28 comparisons, and running it again gives 28 again. Fix the input and randomise the pivot and there is no single number — there is a distribution, and on this input it puts weight on every count from 13 to 28. The lab draws it as bars. E[C] = 2369/140 is one summary of that shape, and P(C = 16) = 73/360 is another.

2

The expectation is computed, not averaged

rqsPmf builds the distribution by recursion on subproblem sizes: D(k) = (k − 1) + (1/k)·Σᵢ D(i) ⊛ D(k − 1 − i), with every weight an exact rational. No run is involved and no seed is consulted, so the answer does not move when the seed slider does. It is checked twice — against the closed form 2(n + 1)Hₙ − 4n, which is a different definition, and at small n against running the fixed-pivot algorithm on all n! input orders, which is a different experiment.

3

Which numbers the input moves, and which it does not

Change the array from sorted to alternating high and low and the fixed-pivot count drops from 28 to 16. The exact distribution does not move at all: not the expectation, not the variance, not a single bar. A uniformly random pivot position selects a uniformly random rank whatever order the keys arrived in, so the randomness is the algorithm's and not the data's, and that is the entire content of randomising the pivot.

The material

Three numbers in one row, and what each one is about

The count on one input, the expectation over every execution, the mean over the seeds shown — and two checks that the middle one is right.

Definition · The two pivot rules, and what a partition costs

Quicksort 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 cost

For 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 full

P(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 form

For 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.

Interactive

Randomised quicksort: one input, a distribution of costs

The fixed rule pays n(n − 1)/2 on this input every time; the random rule has an expectation of 2(n + 1)H − 4n and a spread around it

Randomised quicksort: one input, a distribution of coststhe fixed rule pays the same price every time; the random rule has a distribution, and the mean over seeds is not it
exact, over every executionmeasured, over the seeds shownthe expectationthe worst case, which is always

Method

Reading three columns without mixing them up

Name the kind of each number before you compare any two of them.

1

Ask what the number ranges over

Over one execution on one input, over every execution on one input, or over the seeds on screen. The fixed count is the first, the expectation and the variance are the second, the mean and the observed range are the third. Two numbers of different kinds are never in conflict, however far apart they sit.

2

Move the input and see what follows

Switch between sorted, reversed and alternating. The fixed count moves from 28 to 28 to 16; the whole exact distribution stays put. Anything that moved with the input was a property of the input, and this lab is built so the split is visible in one click.

3

Move the seeds and see what follows

The measured mean and the measured range move; nothing else does. If a sentence you were about to write changes when the seed slider moves, it was a sentence about 120 runs, and it should say so.

4

Check the expectation against something that is not it

The closed form and the enumeration over every input order are two independent routes to the same number, and the panel prints both with a verdict. An expectation computed one way and checked nowhere is the easiest thing on this course to get confidently wrong.

Which number is which

Score 0 / 0

One input, eight keys, three numbers

The array is 1 2 3 4 5 6 7 8, already sorted, and the two rules are run on it. The first column is one execution; the second is all of them; the third is 120 of them.

FIXED PIVOT, last element, on 1..8 [0..7] len 8 pivot 8 7 comparisons [0..6] len 7 pivot 7 6 [0..5] len 6 pivot 6 5 [0..4] len 5 pivot 5 4 [0..3] len 4 pivot 4 3 [0..2] len 3 pivot 3 2 [0..1] len 2 pivot 2 1 --- 28 every time, = n(n-1)/2 RANDOM PIVOT, over every execution E[C] = 2369/140 = 16.921428… = 2(n+1)H_n - 4n, H_8 = 761/280 Var(C) = 160599/19600 = 8.1938… support = 13 .. 28, P(C = 28) = 1/315 RANDOM PIVOT, measured over seeds 1..120 seed 1: 23 seed 2: 13 seed 3: 19 seed 4: 19 seed 5: 18 mean = 415/24 = 17.29166… range 13 .. 27 gap above the expectation = 311/840 = 0.37023…

Seed 2 costs 13 comparisons and seed 1 costs 23, on the same array, with the same algorithm. Neither is the expectation and neither is an error. The distribution puts 1/24 on 13 and 17/1260 on 23, and both events happened in the first two seeds, which is roughly what those probabilities predict.

The gap between 415/24 and 2369/140 is 311/840, about 0.37 comparisons. It is not evidence that the expectation is wrong, and it is not evidence that it is right: with a variance of 8.19 and 120 samples, a gap of that size is ordinary. The next lesson makes that sentence precise without ever taking a square root.

For a faded rehearsal, set the input to alternating and predict all three numbers before the page redraws. The supplied first move is this: the fixed count is a property of the arrangement and will change, and the exact expectation is a property of the algorithm and will not. Say what the measured mean is likely to do — and then say why “likely” is the strongest word available for it.

Common mistakes

1
Running an algorithm many times and calling the average the expectationThe average over 120 seeds on the sorted array is 415/24 and the expectation is 2369/140; on the alternating array the same 120 seeds give 1003/60, which is below it. Three measurements, one expectation, and no amount of extra seeds turns the first kind of number into the second. The expectation is computed from the algorithm's structure, and the lab does it two independent ways.
2
Treating the expectation as the typical costE[C] = 16.92 at n = 8, and the single most likely count is 16 while 17 has less than a fifth of 16's probability. The distribution is jagged at this size and the expectation is a weighted average of a lumpy thing, not a description of a typical run. Look at the bars before saying what a run costs.
3
Quoting a worst case and an expectation as if they compete28 and 2369/140 are both correct about the same algorithm on the same input. The first is an upper bound that holds on every execution and is attained with probability 1/315; the second is the average over all of them. A sentence that puts them in tension has confused a guarantee with a summary.

Check your understanding

✓
Finish when you can say, of every number on a randomised algorithm's page, what it ranges over.

You should be able to name the three kinds apart, predict which of them a change of input moves and which a change of seed moves, explain why the expectation does not depend on the arrangement, and give at least two independent routes to the same expectation.

Note: The variance in the corner of this panel is not decoration. “From an Expectation to a Probability” uses it to bound the chance that a run costs far more than average, and it does so with an inequality that assumes nothing about the jagged shape above — which is the property that makes it worth proving.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.