Skip to content
Part IA Lent Term

Quicksort: Analysis

Worst Case: Θ(n2)\Theta(n^2)

The worst case occurs when the pivot is always the minimum or maximum element of the subarray, producing a split of 00 and n−1n-1 elements. This happens, infamously, when the input is already sorted (or reverse-sorted) with a naive first/last-element pivot choice.

Recurrence:

T(n)=T(n−1)+T(0)+Θ(n)=T(n−1)+Θ(n)T(n) = T(n-1) + T(0) + \Theta(n) = T(n-1) + \Theta(n)

Using the substitution method:

T(n)=T(n−1)+kn=(T(n−2)+k(n−1))+kn=⋯=Θ(1)+k∑i=1ni=Θ(n2)T(n) = T(n-1) + kn = (T(n-2) + k(n-1)) + kn = \dots = \Theta(1) + k\sum_{i=1}^{n} i = \Theta(n^2)

Even a constant-sized split-off (e.g. consistently producing a subarray of size xx and n−x−1n-x-1 for constant xx) still yields Θ(n2)\Theta(n^2), because the recurrence becomes T(n)=T(n−c)+Θ(n)T(n) = T(n-c) + \Theta(n).

Best Case: Θ(nlog⁡n)\Theta(n \log n)

If the pivot always splits the array exactly in half:

T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n)

By the Master Theorem (Case 2, a=2,b=2,f(n)=Θ(n)=Θ(nlog⁡22)a=2, b=2, f(n)=\Theta(n) = \Theta(n^{\log_2 2})):

T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n)

Ratio Splits Also Give Θ(nlog⁡n)\Theta(n \log n)

A split of 1/41/4 to 3/43/4 still produces Θ(nlog⁡n)\Theta(n \log n). The recursion tree has depth log⁡4/3n\log_{4/3} n on the longest path and log⁡4n\log_4 n on the shortest, with at most knkn work per level. In both cases:

T(n)≤kn⋅log⁡4/3n=Θ(nlog⁡n)T(n) \le kn \cdot \log_{4/3} n = \Theta(n \log n)

An ratio split (any constant fraction on both sides, no matter how unbalanced) guarantees Θ(nlog⁡n)\Theta(n \log n). Only constant-sized splits degrade to Θ(n2)\Theta(n^2).

Average Case: Θ(nlog⁡n)\Theta(n \log n)

Assuming random pivot selection (all permutations equally likely), the expected number of comparisons is approximately 2nln⁡n≈1.39 nlog⁡2n2n \ln n \approx 1.39 \, n \log_2 n.

Intuition: any two elements ziz_i and zjz_j (with i<ji < j in sorted order) are compared if and only if one of them is chosen as a pivot before any element between them in sorted order. The probability they are compared is 2j−i+1\frac{2}{j-i+1}. Summing over all pairs:

E[comparisons]=∑i=1n−1∑j=i+1n2j−i+1=∑i=1n−1∑k=2n−i+12k≤2n∑k=1n1k≈2nln⁡n+O(n)\mathbb{E}[\text{comparisons}] = \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} \frac{2}{j-i+1} = \sum_{i=1}^{n-1} \sum_{k=2}^{n-i+1} \frac{2}{k} \le 2n \sum_{k=1}^{n} \frac{1}{k} \approx 2n \ln n + O(n)

Thus the expected running time is Θ(nlog⁡n)\Theta(n \log n).

Comparison with MergeSort

PropertyQuicksortMergeSort
Worst caseΘ(n2)\Theta(n^2)Θ(nlog⁡n)\Theta(n \log n)
Average caseΘ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)
Extra spaceO(log⁡n)O(\log n) (stack)Θ(n)\Theta(n) (merge arrays)
StableNoYes
Cache behaviourExcellent (in-place, sequential access)Good but external merge requires extra memory

Despite the Θ(n2)\Theta(n^2) worst case, Quicksort is often faster than MergeSort in practice because of low constant factors and cache-friendly memory access patterns. The worst case can be mitigated by random pivot selection or median-of-three.

Summary

CaseRunning TimeCondition
BestΘ(nlog⁡n)\Theta(n \log n)Pivot always splits evenly
AverageΘ(nlog⁡n)\Theta(n \log n)Random pivot; expected ∼2nln⁡n{\sim}2n \ln n comparisons
WorstΘ(n2)\Theta(n^2)Pivot always min or max
Ratio splitΘ(nlog⁡n)\Theta(n \log n)Any constant fraction split
Constant splitΘ(n2)\Theta(n^2)Splitting off constant-size chunk
Key insightUnbalanced but proportional splits are fine; constant-remainder splits are disastrous