3.6.4 · D1Sorting & Searching

Foundations — Quick sort randomization — expected O(n log n)

2,623 words12 min readBack to topic

Before you can read the parent proof you need to be fluent with a handful of symbols and pictures. We build them one at a time, each earning its place before the next appears. Nothing below assumes you already know it.


0. A standing assumption — distinct elements


1. The array and "sorted order" — what means

An array is just a row of boxes holding numbers, in whatever messy order they arrived.

The figure below shows a messy input on top and the same values relabelled by rank underneath — trace the pink arrow to see how the physical value (sitting in slot 5) becomes the rank .

Figure — Quick sort randomization — expected O(n log n)

Why the topic needs this. Quicksort never sorts by touching "position 3"; it sorts by value comparisons. Talking about (rank) instead of "the number in slot 3" lets us reason about which values get compared, independent of how jumbled the input was. That independence is exactly what randomization buys us.


2. What a comparison is, and the pivot

In the figure, the yellow box is the pivot (). Follow the blue and pink arrows: everything smaller drops into the left chunk, everything larger into the right chunk. The caption underneath states the key consequence.

Figure — Quick sort randomization — expected O(n log n)

3. Probability — "what fraction of the time?"

Why this tool and not another. We can't predict the coin flips, so we can't say exactly how many comparisons happen on one run. Probability is the language for "on average, across all possible coin flips". That's the only honest thing to measure when the algorithm is random.


4. Expected value — the average outcome

We introduce before using it, because the "magic bridge" in the next section leans on it.


5. Random variables and the indicator


6. Linearity of expectation — adding the pieces

Why this tool. The total comparison count is , a sum of thousands of tangled indicators. Computing directly is hopeless. Linearity lets us push inside the sum and handle each pair alone: This is the single most important move in the proof.


7. The gap and why probability depends only on it

Look again at the two elements and . Between them (in sorted order) sits the set which has elements.

In the figure, the whole set is ringed; the two yellow ends are the "good" elements and the blue middles are the ones that split the pair apart. The double-headed pink arrow marks the gap that sets the count .

Figure — Quick sort randomization — expected O(n log n)

Notice this depends only on the gap , not on where the pair sits. That's why the parent substitutes to simplify the double sum.


8. The harmonic number and

The figure draws the curve in yellow with a blue rectangle of height and width for each term — the rectangles' total area is , and it hugs the area under the curve, which is .

Figure — Quick sort randomization — expected O(n log n)

9. Collapsing the sum into

Now watch the double sum turn into the harmonic number — this is the algebra the parent asserts.

Why the topic ends here. The final bound is just the pair-probabilities summed and simplified. Recognising is what turns that sum into the headline result .


Prerequisite map

Distinct values and sorted rank z_i

Pivot and comparison

Pair compared at most once

Probability as good over total

Expected value as average

Indicator X_ij is 0 or 1

Expected value equals probability

Linearity of expectation

Sum of pair probabilities

Gap k feeds prob 2 over k plus 1

Collapse to 2 times n minus 1 times H_n

Harmonic number H_n

H_n grows like log n

Expected O of n log n

Everything on the left builds the machinery; the two threads (indicators + linearity) and (the gap probability) merge into the single sum, which the harmonic number collapses into the final bound. Full proof: the parent topic. Compare with Quicksort (deterministic) and Merge Sort; the flavour of "random but always-correct" is Las Vegas vs Monte Carlo algorithms.


Equipment checklist

Cover the right side and test yourself — if any answer is fuzzy, re-read that section before the main proof.

Why do we assume distinct input values?
So each element has a unique rank and we can write with strict ; ties are handled by a fixed tie-break, so it costs nothing.
What does mean?
The -th smallest value (a rank), regardless of its position in the unsorted array.
When are two elements ever compared in quicksort?
Only when one of them is the pivot, while both are still in the same chunk — so at most once.
What does the notation mean?
The event that and are compared at some point during the algorithm.
Why do base-case chunks (size 0 or 1) not hide any comparisons?
They do zero comparisons, so every real comparison happens against a pivot in a chunk of size and is captured by some .
What is the expected value of a variable that is with probability ?
Exactly .
What is an indicator random variable ?
A variable that is if , otherwise — nothing in between.
Why is ?
The average of a variable equals the probability it is .
State linearity of expectation.
, true even when the are dependent.
Why is ?
Of the elements between them, each equally likely to be first pivot, exactly (the ends) cause a comparison.
How does collapse to ?
Sub , bound the inner sum by , and sum the -independent bound over values of .
What is the harmonic number ?
, the sum of reciprocals of through .
How does relate to ?
, i.e. , because it's the area under .
What does mean?
Work grows at most like times a slowly-growing logarithm, ignoring constants.