LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

Why is quick-sort's expected running time $O(n \log n)$ when the pivot is picked at random?

A random pivot falls into the middle half of the values with probability 1/2, and such a "good" pivot leaves both $L$ and $G$ at most 3/4 as large as the input. So on average every second split shrinks a sequence by a constant factor, the expected depth of the tree is $O(\log n)$, and each level costs $O(n)$.

Call a split good if its pivot lies between the 25 % mark and the 75 % mark of the sorted order: then neither $L$ nor $G$ can have more than 3/4 of the elements. Half of all elements are such pivots, so a random pick is good with probability 1/2.

Follow any element on its way down the tree. Its sequence shrinks to at most 3/4 at every good split, so after $\log_{4/3} n$ good splits it is down to a single element. As each split is good with probability 1/2, that takes about $2 \log_{4/3} n$ levels on average, which is $O(\log n)$. All calls on one level together handle at most $n$ elements, so the expected total is $O(n) \cdot O(\log n) = O(n \log n)$.

The randomness lies in the algorithm, not in the input: for a randomly pivoting quick-sort no input is bad, only some runs are unlucky, and long unlucky streaks are very improbable. A fixed rule such as "first element" only achieves $O(n \log n)$ on average over inputs in random order, that is, when the values are evenly spread over the sequence; a particular input can still trigger the worst case every single time.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026