3.6.4 · D5Sorting & Searching
Question bank — Quick sort randomization — expected O(n log n)
Before we start, five names we'll lean on (all built in the parent note):
- Pivot — the element we partition around; everything smaller goes left, larger goes right.
- Comparison — one "is ?" check; the parent proved all quicksort work is comparisons.
- — the -th smallest element in sorted order (a rank label, not a position in the input).
- — an indicator: it equals if and are ever compared during the run, and otherwise. Because a pair is compared at most once, .
- — the total number of comparisons in a run, . Its expectation is the quantity the whole analysis bounds ().
- — the set of the elements lying between and (inclusive) in sorted order.
True or false — justify
Randomized quicksort's worst-case running time is .
False — the worst case is still (you could flip unlucky coins every level); only the expected time is .
Randomization makes quicksort's expected time only for random inputs.
False — the expectation is over the algorithm's coin flips, so it is for every fixed input, including sorted or adversarial ones.
Two elements and can be compared several times during a run.
False — a comparison only happens when one of them is the pivot, and pivots are removed from all future subarrays, so each pair is compared at most once.
Adjacent elements and in sorted order are always compared.
True — ; nothing sits between them, so whichever becomes a pivot first must compare against the other.
Shuffling the array once at the start is strictly weaker than picking random pivots.
False — a single uniform shuffle then deterministic pivoting gives the same expected ; both make each pivot's rank uniformly random.
The probability depends on where and sit in the input array.
False — it depends only on their sorted-order gap ; the input's physical arrangement is irrelevant after randomization.
Randomized quicksort is a Las Vegas algorithm.
True — it always returns the correct sorted output; only the running time is random. Compare with Las Vegas vs Monte Carlo algorithms.
If two elements land in different partitions after some pivot, they might still be compared later.
False — once a pivot splits them apart, they live in disjoint subarrays forever and are never compared again.
Spot the error
"Since we proved , a run of length is impossible."
The error: expectation is an average, not a maximum. An run is possible but improbable — the bound only pins the average over coin flips.
" can be because could be a pivot once and a pivot later."
The error: after is a pivot it is removed, so it can never be compared to again; each pair contributes at most one comparison, so .
"The first pivot chosen anywhere in the whole array decides whether are compared."
The error: only the first pivot drawn from the set matters; a pivot outside that range keeps and together and settles nothing.
" because there are candidates."
The error: there are two favourable first-pivots ( or ), not one, so the numerator is , giving .
"Random pivots guarantee a balanced split at every level."
The error: they guarantee balance on average, not always — a random pivot can still be an extreme; the analysis sums probabilities, it doesn't promise each split is even.
"Because the harmonic number grows, is ."
The error: , not linear; the bound is , with only one factor of outside the log.
"Choosing the median as pivot every time is the same idea as randomization."
The error: exact median selection (Median of Medians) is a deterministic guarantee of balance; randomization achieves good balance in expectation far more cheaply, without computing a median.
Why questions
Why do we count comparisons instead of, say, swaps or array writes?
Because comparisons dominate quicksort's total work — bounding their expected number bounds the whole runtime up to constant factors.
Why can we split into a sum of per-pair probabilities?
Why does every element of have an equal chance of being the first pivot picked from it?
Pivots are drawn uniformly at random, so within any fixed subset the "first to be chosen" is uniform over that subset — that symmetry is the entire payoff of randomization.
Why does the adversary lose once we randomize the pivot?
The cost now depends on our coin flips, not on how the input is arranged; an adversary would have to predict the randomness, which they cannot.
Why is the harmonic number the natural quantity here?
After substituting the gap , the inner sum becomes , which is bounded by ; see Harmonic Number for .
Why is randomized quicksort often preferred over Merge Sort despite the same ?
Quicksort sorts in place with better cache behaviour and small constants; merge sort needs extra memory, though it guarantees worst case.
Why doesn't sorted input [1,2,3,4,5] cause the blow-up anymore?
The pivot's rank is now uniformly random regardless of input order, so the split is balanced in expectation; the input being pre-sorted carries no information about the pivot.
Edge cases
What is the expected number of comparisons when ?
Zero — a single-element (or empty) subarray triggers no partition, so no comparisons occur.
What happens if the subarray has all equal elements?
Behaviour depends on the partition scheme; naive Lomuto can degrade to on all-equal keys, which is why three-way ("Dutch flag") partitioning is used — randomizing the pivot alone doesn't fix duplicates.
Is the pair ever compared when the very first pivot of the whole array is some with ?
No — that pivot immediately separates and into different sides, so they are never compared: this is exactly the "middle pivot" losing case.
For the farthest pair , what is the probability they are compared, and is it the smallest such probability?
, the minimum over all pairs, since is as large as possible — distant elements are the least likely to meet.
If we always pick the pivot to be the true median (never random), what is the running time?
A guaranteed worst case — but this needs deterministic median selection each level and larger constants; randomization trades the guarantee for cheaper expected performance.
Does the expectation still hold if the random pivot is chosen from only the first three elements?
Not for arbitrary input — "median-of-three from fixed positions" is deterministic and can be adversarially defeated; the guarantee needs the pivot's rank to be (near) uniform, which fixed-position sampling does not ensure.
Recall One-line summary to lock in
Randomization moves the risk from which input to which coin flips — worst case survives but becomes astronomically unlikely, and the expected cost is on every input, driven entirely by the pair-comparison probability.