Intuition What this page is for
The parent note proved the formula. Here we hunt down every scenario the formula can be applied to — tiny arrays, the smallest non-trivial case, adjacent vs. far-apart pairs, sorted "adversary" input, duplicate elements, degenerate one-element arrays, a real-world word problem, and an exam twist. Each worked example is tagged with the matrix cell it covers, so by the end you've seen the machinery run in every corner.
Before we start, one reminder of the single formula everything below uses. From the parent note :
The key numbers here are the gap d = j − i (how far apart two elements sit in sorted order) and the resulting probability d + 1 2 . Notice: the probability depends only on the gap , never on the actual values or where the elements started. Keep that in your pocket.
Definition The between-set
Z ij (used in Ex 6 and Ex 9)
Z ij = { z i , z i + 1 , … , z j }
is the set of all sorted elements from z i up to z j inclusive — the two endpoints plus everything strictly between them. It has j − i + 1 members. The parent note's key fact is: whichever member of Z ij is chosen as a pivot first decides everything. If that first pivot is an endpoint (z i or z j ), the pair gets compared; if it's a middle element, the pair is split apart forever. Two "good" choices out of j − i + 1 equally likely ones gives the j − i + 1 2 probability.
Definition The harmonic number
H n (used from Ex 7 onward)
H n = ∑ k = 1 n k 1 = 1 + 2 1 + 3 1 + ⋯ + n 1
It grows like the natural logarithm: H n ≈ ln n + γ , where γ ≈ 0.5772 is Euler's constant . A sharper estimate keeps the next term, H n ≈ ln n + γ + 2 n 1 , and the leftover error after that is smaller than 8 n 2 1 — negligible for the sizes we use. See Harmonic Number for the full story. Whenever we say "O ( log n ) " for quicksort, this H n is where the logarithm secretly comes from.
Every question this topic can throw at you falls into one of these cells. The worked examples below are labelled with the cell (C1–C9) they cover.
Cell
Scenario class
What's tricky about it
Example
C1
Smallest non-trivial array (n = 2 , 3 )
Are adjacent elements always compared?
Ex 1
C2
Full expected count for small n
Grouping pairs by gap
Ex 2
C3
Gap extremes — adjacent (d = 1 ) vs far (d max)
Pr = 1 vs Pr tiny
Ex 3
C4
"Adversary" sorted / reverse-sorted input
Does input order change the answer?
Ex 4
C5
Degenerate: n = 1 and n = 0
Zero comparisons, base case
Ex 5
C6
Duplicates / equal keys
Does "sorted order" still make sense?
Ex 6
C7
Limiting behaviour (large n )
H n → ln n , ratio to n ln n
Ex 7
C8
Real-world word problem
Translate a story into the formula
Ex 8
C9
Exam twist — conditional / "given first pivot"
Reasoning under a condition
Ex 9
n = 2 and n = 3 : how many comparisons on average?
Compute E [ X ] for n = 2 , then for n = 3 .
Forecast: For two elements, guess: is the number of comparisons random at all, or is it fixed? For three, will it be a nice fraction or an integer?
Step 1 — n = 2 : list the pairs. Only one pair exists: ( z 1 , z 2 ) , gap d = 2 − 1 = 1 .
Why this step? E [ X ] is a sum over pairs i < j ; with two elements there is exactly one pair to sum.
Step 2 — apply the formula. Pr [ z 1 ↔ z 2 ] = d + 1 2 = 2 2 = 1 . So E [ X ] = 1 .
Why this step? Gap-1 (adjacent) elements are compared with probability 1 — one of them must become the pivot before they can be split apart, and comparing against the pivot is unavoidable.
Step 3 — n = 3 : list all pairs. ( z 1 , z 2 ) gap 1, ( z 2 , z 3 ) gap 1, ( z 1 , z 3 ) gap 2.
Why this step? With three elements there are ( 2 3 ) = 3 pairs; we need all of them.
Step 4 — sum the probabilities.
E [ X ] = ( 1 , 2 ) 2 2 + ( 2 , 3 ) 2 2 + ( 1 , 3 ) 3 2 = 1 + 1 + 3 2 = 3 8 ≈ 2.667
Why this step? Linearity of Expectation lets us add each pair's probability directly — no independence needed, which is the whole reason this method is so clean.
Verify: For n = 2 , sorting two elements always takes exactly one comparison, no matter the pivot — so E [ X ] = 1 is not just an average, it's a certainty. For n = 3 : minimum possible is 2 comparisons (if the middle element z 2 is picked first, it splits cleanly, then each side of size 1 costs 0 more... but partitioning still compares z 2 against both others = 2). If an extreme is picked first (prob 2/3 ), you get 3 comparisons. So E [ X ] = 3 1 ( 2 ) + 3 2 ( 3 ) = 3 2 + 6 = 3 8 . ✅ Matches.
Worked example Expected comparisons for a 5-element array
Compute E [ X ] for n = 5 by grouping pairs by gap.
Forecast: There are ( 2 5 ) = 10 pairs. Guess whether the answer is closer to 5, 7, or 10.
Step 1 — count how many pairs have each gap d . In a line of 5 positions, the number of pairs with gap d is 5 − d :
d = 1 : 4 pairs
d = 2 : 3 pairs
d = 3 : 2 pairs
d = 4 : 1 pair
Why this step? The probability d + 1 2 depends only on the gap, so we batch pairs by gap and multiply by how many share that gap — far faster than listing all 10.
Step 2 — multiply each group by its probability.
E [ X ] = 4 ⋅ 2 2 + 3 ⋅ 3 2 + 2 ⋅ 4 2 + 1 ⋅ 5 2
Why this step? Each of the 5 − d pairs at gap d contributes the same d + 1 2 ; Linearity of Expectation says just add them.
Step 3 — arithmetic.
= 4 ( 1 ) + 3 ( 3 2 ) + 2 ( 2 1 ) + 5 2 = 4 + 2 + 1 + 0.4 = 7.4
Verify: Two independent sanity checks. (a) Direct enumeration: literally adding all 10 pair-probabilities d + 1 2 one by one also gives 7.4 (done in Linearity of Expectation style). (b) Below the parent's bound: the parent note derived the loose bound E [ X ] < 2 ( n − 1 ) H n by replacing each k + 1 2 with the strictly larger k 2 and extending the inner sum to H n . With H 5 = 1 + 2 1 + 3 1 + 4 1 + 5 1 = 60 137 ≈ 2.2833 , that bound is 2 ( 4 ) ( 2.2833 ) = 18.27 , and indeed 7.4 < 18.27 ✅. The bound is deliberately loose — Ex 7 upgrades it to a tight closed form. Also 7.4 sits between 5 and 10 — reasonable for 10 pairs where far-apart ones rarely meet.
Worked example Adjacent vs. far-apart in one array of
n = 9
Compare Pr [ z 1 ↔ z 2 ] (adjacent) against Pr [ z 1 ↔ z 9 ] (the two extremes).
Forecast: One of these is a guarantee and one is rare . Which is which, and roughly how rare?
Step 1 — adjacent pair, gap d = 1 . Pr [ z 1 ↔ z 2 ] = 1 + 1 2 = 1 .
Why this step? The figure below draws all nine sorted positions z 1 , … , z 9 as black dots on a line. The accent-red short arrow above marks the adjacent pair z 1 , z 2 (leftmost two dots), which has nothing between them — so the only way to separate them into different subarrays would be a pivot of value strictly between z 1 and z 2 , which does not exist. Before they can ever part, one of the two must itself be chosen as pivot, forcing the comparison. Probability 1. Contrast this with the extremes z 1 and z 9 : the red double-arrow below spans a wide gap with seven black dots (z 2 , … , z 8 ) sitting between them, any one of which can wedge in as a pivot first.
Figure: Nine sorted positions as black dots. Red short arrow (top) = adjacent pair z 1 , z 2 , gap 1, probability 1. Red wide arrow (bottom) = extreme pair z 1 , z 9 , gap 8, probability 2/9 — the seven middle dots between them can each split the pair apart.
Step 2 — extreme pair, gap d = 8 . Pr [ z 1 ↔ z 9 ] = 8 + 1 2 = 9 2 ≈ 0.2222 .
Why this step? Now there are 7 middle elements z 2 , … , z 8 (the black dots between the red endpoints in the figure). If any of them is chosen as a pivot before z 1 or z 9 , the two extremes get thrown into opposite partitions and never meet. Only 2 of the 9 candidates (z 1 or z 9 ) are "good".
Step 3 — ratio. The adjacent pair is 2/9 1 = 4.5 × more likely to be compared than the extreme pair.
Verify: As the gap grows, d + 1 2 → 0 — far-apart elements almost never meet, which is exactly why the total stays O ( n log n ) and not O ( n 2 ) : most of the ( 2 n ) pairs contribute tiny probabilities. ✅
[1,2,3,4,5] vs. shuffled — does E [ X ] change?
Deterministic last-element-pivot quicksort dies on sorted input (O ( n 2 ) ). What is E [ X ] for randomized quicksort on the same sorted input [1,2,3,4,5]?
Forecast: Will sorted input give a bigger E [ X ] than random input, or exactly the same?
Step 1 — identify the sorted order. The input [1,2,3,4,5] is already sorted, so z 1 = 1 , … , z 5 = 5 .
Why this step? The formula is stated in terms of the sorted positions z i , so we must map values to positions first.
Step 2 — realise the formula never mentions input order. E [ X ] = ∑ i < j j − i + 1 2 depends only on n and the gaps, not on how the array was arranged when it arrived.
Why this step? Random pivoting makes each pivot's rank uniformly random regardless of layout — this is the entire point of randomization (it "breaks the link between input order and pivot rank", per the parent note). The adversary's carefully sorted array is now no different from a shuffled one.
Step 3 — reuse Ex 2's computation. Same n = 5 , so E [ X ] = 7.4 .
Verify: Deterministic last-pivot on this sorted input compares the pivot against every remaining element each level: 4 + 3 + 2 + 1 = 10 comparisons (its worst case). Randomized expects only 7.4 — strictly less, and crucially the 7.4 holds for every input, including this "adversarial" one. Randomization removed the bad input , not the bad luck . ✅ (See Quicksort (deterministic) for the version that actually suffers here, and Las Vegas vs Monte Carlo algorithms — this is a Las Vegas algorithm: always correct, running time random.)
Worked example The base cases
What is E [ X ] for an empty array (n = 0 ) and a single-element array (n = 1 )?
Forecast: Zero, one, or "undefined"?
Step 1 — n = 1 : count the pairs. The sum ∑ i < j needs two distinct indices; with one element there is no pair i < j .
Why this step? An empty sum is 0 by convention — nothing to add.
Step 2 — n = 0 : same reasoning. No elements, no pairs, E [ X ] = 0 .
Why this step? This confirms the recursion's base case: QUICKSORT(A, lo, hi) does nothing when lo >= hi, i.e. subarrays of size ≤ 1 trigger zero comparisons.
Verify: A list of length 0 or 1 is already sorted , so of course no comparisons are needed. The formula gracefully returns 0 instead of dividing by anything dangerous — no degenerate blow-up. ✅ Also: 2 ( n − 1 ) H n at n = 1 gives 2 ( 0 ) H 1 = 0 , consistent.
Worked example Does the analysis survive equal elements?
[7,7,7]
Estimate the comparison count for three equal keys, and explain whether the sorted-order labelling still makes sense.
Forecast: Do duplicates break the "z 1 < z 2 < z 3 " strict-inequality setup?
Step 1 — break ties by original position. Equal keys can't be ordered by value, so we impose a tie-broken total order written ⪯ : for two equal keys, the one at the smaller array index comes first. Under this rule [7,7,7] becomes z 1 , z 2 , z 3 with z 1 before z 2 before z 3 . The symbol ⪯ is just "< " extended to also order equal values by index, so every pair has a definite "earlier" and "later".
Why this step? The proof only needs a fixed total order so that "the first member of Z ij chosen as a pivot" (see the Z ij definition above) is well-defined. Tie-breaking by index supplies one, and the formula's upper bound still applies.
Step 2 — apply the n = 3 result as an upper bound. With the tie-broken order, the same structure as Ex 1 (n = 3 ) gives E [ X ] ≤ 3 8 ≈ 2.667 .
Why this step? Any real partitioning of equal keys does at most as many comparisons as the distinct-key analysis predicts (a good three-way partition on equal keys can do even fewer).
Verify: In practice, a naive two-way partition on all-equal input compares the pivot against every other element every level. For n = 3 that's exactly 2 comparisons (pivot vs. two others, one level, since both go to the same side). 2 ≤ 2.667 ✅ — the bound holds. The moral: duplicates never make the expected comparison count exceed the distinct-key formula.
E [ X ] grow, and how tight is the constant?
Estimate E [ X ] for n = 1000 and find the ratio n l n n E [ X ] .
Forecast: Guess the ratio: is it near 0.5 , 1 , or 2 ?
Step 1 — set up the double sum and substitute k = j − i . Start from
E [ X ] = ∑ i = 1 n − 1 ∑ j = i + 1 n j − i + 1 2 .
Fix i and let k = j − i be the gap. As j runs from i + 1 up to n , the gap k runs from 1 up to n − i , and the term becomes k + 1 2 :
E [ X ] = ∑ i = 1 n − 1 ∑ k = 1 n − i k + 1 2 .
Why this step? Rewriting in terms of the gap k makes the summand depend on k alone — the inner term no longer mentions i , which is what lets us swap the order of summation next.
Step 2 — swap the summation order (the counting argument). We want to sum over k on the outside. For a fixed gap k , which values of i produce that gap? The condition is k ≤ n − i , i.e. i ≤ n − k , and i ≥ 1 . So i ranges over 1 , 2 , … , n − k — exactly n − k values of i . Each of those contributes the same term k + 1 2 , so summing over them multiplies by n − k :
E [ X ] = ∑ k = 1 n − 1 ( n − k ) k + 1 2 .
Why this step? This is the missing counting step: "how many ( i , k ) pairs share a given gap k ?" The answer n − k is exactly the number of pairs of positions that are k apart in a line of n — the same count we used in Ex 2 when we said "5 − d pairs at gap d ".
Step 3 — split the fraction and evaluate to a closed form. Write k + 1 n − k = k + 1 ( n + 1 ) − ( k + 1 ) = k + 1 n + 1 − 1 . Summing the "− 1 " over k = 1 to n − 1 gives − ( n − 1 ) ; summing k + 1 n + 1 gives 2 ( n + 1 ) ( H n − 1 ) (the shifted harmonic sum ∑ k = 1 n − 1 k + 1 1 = H n − 1 ). Combining and simplifying yields the exact identity
E [ X ] = 2 ( n + 1 ) H n − 4 n
Why this step? The parent's bound 2 ( n − 1 ) H n came from rounding up k + 1 2 → k 2 ; here we keep the exact k + 1 2 and evaluate honestly, so we get an equality. Both are O ( n log n ) — but the boxed form is exact and lets us read off the true constant. (This identity is machine-checked against the raw double sum in the verifier.)
Step 4 — plug n = 1000 using H n ≈ ln n + γ . From the harmonic-number definition, H 1000 ≈ ln 1000 + 0.5772 = 6.9078 + 0.5772 = 7.4850 (the 2 n 1 = 0.0005 correction is negligible here). Then
E [ X ] ≈ 2 ( 1001 ) ( 7.4850 ) − 4000 = 14985.0 − 4000 = 10985.
Why this step? Concrete numbers make "asymptotic" tangible.
Step 5 — compute the ratio n l n n E [ X ] . n ln n = 1000 × 6.9078 = 6907.8 , so
n l n n E [ X ] ≈ 6907.8 10985 ≈ 1.59.
Why this step? The leading term of E [ X ] is 2 ( n + 1 ) H n ∼ 2 n ln n , so the ratio should approach 2 ; getting 1.59 at n = 1000 shows it is climbing toward 2 slowly (the slowness is the ln n 's doing).
Verify: 2 ( n + 1 ) H n − 4 n ∼ 2 n ln n , so dividing by n ln n should tend to 2 . At n = 1000 we get 1.59 — below 2 but climbing, exactly as the slow ln n growth predicts ✅. The verifier also confirms the boxed identity equals the raw double sum at n = 10 and n = 1000 . Compare Merge Sort , which does ≈ n log 2 n ≈ 9966 comparisons here — same O ( n log n ) class, different constant.
Worked example Sorting exam scores
A teacher randomized-quicksorts 50 exam scores. Estimate the average number of score comparisons the algorithm performs.
Forecast: Closer to 50 , 250 , or 1250 ?
Step 1 — translate the story into symbols. "50 scores" ⇒ n = 50 . "Average number of comparisons" ⇒ E [ X ] . Any tied scores are handled by the tie-broken order of Ex 6, so the formula still applies.
Why this step? Word problems must be mapped to symbols before any formula runs; misidentifying n is the classic error.
Step 2 — approximate H 50 with its correction term. From the harmonic-number definition, the sharper estimate is H n ≈ ln n + γ + 2 n 1 . Here the extra term is 2 ⋅ 50 1 = 100 1 = 0.01 , which matters at this modest size:
H 50 ≈ ln 50 + 0.5772 + 0.01 = 3.9120 + 0.5772 + 0.01 = 4.4992 ≈ 4.499.
Why this step? We include 2 n 1 because at n = 50 dropping it would cost about 0.01 in H n and roughly a whole comparison in the final answer — the correction earns its place. (The true value H 50 = 4.4992 confirms the estimate is essentially exact.)
Step 3 — apply the exact closed form from Ex 7. E [ X ] = 2 ( n + 1 ) H n − 4 n = 2 ( 51 ) ( 4.499 ) − 200 = 458.9 − 200 = 258.9 ≈ 259 .
Why this step? We reuse the boxed identity rather than the loose bound, so the estimate is tight.
Step 4 — interpret. On average about 259 comparisons to sort 50 scores.
Verify: Cross-check against the parent's loose bound 2 ( n − 1 ) H n = 2 ( 49 ) ( 4.499 ) = 440.9 : our 259 < 441 ✅. And 259 sits near the 250 forecast bucket ✅. A naive O ( n 2 ) scheme would risk ( 2 50 ) = 1225 comparisons, so randomization keeps us near 259 instead — units check out (both sides are counts of comparisons).
Worked example "Given the first pivot of the whole array is
z 5 , are z 1 and z 9 ever compared?" (n = 9 )
Reason about z 1 ↔ z 9 conditioned on z 5 being the very first pivot chosen for the entire array.
Forecast: Is the conditional probability 0 , 9 2 , or something in between?
Step 1 — see what picking z 5 first does. z 5 splits everything into a left subarray { z 1 , … , z 4 } and a right subarray { z 6 , … , z 9 } .
Why this step? The first pivot partitions by rank; anything smaller than z 5 goes left, anything larger goes right. So z 1 lands left and z 9 lands right .
Step 2 — check whether they can ever meet again. Once separated into different subarrays, quicksort never compares elements across the split. So Pr [ z 1 ↔ z 9 ∣ first pivot = z 5 ] = 0 .
Why this step? This is precisely the "middle pivot" losing case from the parent's derivation, but here the conditioning forces the middle pivot to be chosen first.
Step 3 — reconcile with the unconditional 9 2 . The unconditional Pr = 9 2 averages over which member of Z 19 = { z 1 , … , z 9 } is picked first. z 5 is one of the 7 "bad" middles; conditioning on a bad middle gives 0 . Conditioning on z 1 or z 9 first would give 1 .
Verify: Check the average matches: each of the 9 members of Z 19 is equally likely (9 1 ) to be "first pivot among Z 19 ". The 2 good ones give 1 , the 7 bad give 0 : 9 1 ( 2 ⋅ 1 + 7 ⋅ 0 ) = 9 2 ✅ — exactly the unconditional probability. The condition just fixed one of the coin flips.
Recall Which matrix cell does each fact hit? (open after attempting)
Adjacent elements (d = 1 ) are compared with probability… ::: exactly 1 (Cell C3/C1).
E [ X ] for n = 5 … ::: 7.4 comparisons (Cell C2).
Does sorted input raise E [ X ] for randomized quicksort? ::: No — the formula ignores input order; still 7.4 for n = 5 (Cell C4).
E [ X ] for n = 0 or n = 1 … ::: 0 (empty sum, Cell C5).
Do duplicate keys break the analysis? ::: No — tie-break by index; the distinct-key formula is an upper bound (Cell C6).
Exact closed form for E [ X ] ? ::: 2 ( n + 1 ) H n − 4 n (Cell C7).
Ratio E [ X ] / ( n ln n ) as n → ∞ … ::: tends to 2 (Cell C7).
Given the whole array's first pivot is a middle element z k with i < k < j , Pr [ z i ↔ z j ] … ::: 0 — they are split apart forever (Cell C9).
"Neighbours never miss, strangers rarely kiss." Gap 1 → probability 1; big gap → probability d + 1 2 → 0 .