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 n1n-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(n1)+T(0)+Θ(n)=T(n1)+Θ(n)T(n) = T(n-1) + T(0) + \Theta(n) = T(n-1) + \Theta(n)

Using the substitution method:

T(n)=T(n1)+kn=(T(n2)+k(n1))+kn==Θ(1)+ki=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 nx1n-x-1 for constant xx) still yields Θ(n2)\Theta(n^2), because the recurrence becomes T(n)=T(nc)+Θ(n)T(n) = T(n-c) + \Theta(n).

Best Case: Θ(nlogn)\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)=Θ(nlog22)a=2, b=2, f(n)=\Theta(n) = \Theta(n^{\log_2 2})):

T(n)=Θ(nlogn)T(n) = \Theta(n \log n)

Ratio Splits Also Give Θ(nlogn)\Theta(n \log n)

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

T(n)knlog4/3n=Θ(nlogn)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 Θ(nlogn)\Theta(n \log n). Only constant-sized splits degrade to Θ(n2)\Theta(n^2).

Average Case: Θ(nlogn)\Theta(n \log n)

Assuming random pivot selection (all permutations equally likely), the expected number of comparisons is approximately 2nlnn1.39nlog2n2n \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 2ji+1\frac{2}{j-i+1}. Summing over all pairs:

E[comparisons]=i=1n1j=i+1n2ji+1=i=1n1k=2ni+12k2nk=1n1k2nlnn+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 Θ(nlogn)\Theta(n \log n).

Comparison with MergeSort

PropertyQuicksortMergeSort
Worst caseΘ(n2)\Theta(n^2)Θ(nlogn)\Theta(n \log n)
Average caseΘ(nlogn)\Theta(n \log n)Θ(nlogn)\Theta(n \log n)
Extra spaceO(logn)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Θ(nlogn)\Theta(n \log n)Pivot always splits evenly
AverageΘ(nlogn)\Theta(n \log n)Random pivot; expected 2nlnn{\sim}2n \ln n comparisons
WorstΘ(n2)\Theta(n^2)Pivot always min or max
Ratio splitΘ(nlogn)\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