Sub topic 6
How Fast Is Quicksort? Best, Average, and Worst Case
This chapter assumes you can read Big-O notation, follow a recurrence like T(n) = 2T(n/2) + n, and know the Lomuto quicksort from the earlier chapters. We count comparisons. Swaps never outnumber comparisons, so the comparison count sets the running time.
The cost of one partition
Partitioning a range of n elements compares each non-pivot element with the pivot once, which makes n − 1 comparisons. The total cost therefore depends on one thing: how evenly each pivot splits its range.
Best case: even splits
If every pivot lands in the middle, each call creates two halves:
T(n) = 2T(n/2) + Θ(n)
Every level of the recursion tree does about n work in total, and there are log₂ n levels before the ranges reach size 1.
Total: Θ(n log n).
Worst case: one empty side
Suppose every pivot is the smallest or largest element in its range. One side is empty and the other has n − 1 elements:
T(n) = T(n − 1) + Θ(n)
Adding up the comparisons gives (n − 1) + (n − 2) + … + 1 = n(n − 1)/2, so the total is Θ(n²). The recursion depth also grows to n, which matters in Python because its default recursion limit is about 1000.
Average case: a probability argument
Assume the pivot is a uniformly random element of its range. That happens with a random pivot, or with a fixed pivot on input in random order. Name the elements in sorted order z₁ < z₂ < … < zₙ.
- Two elements zᵢ and zⱼ (with i < j) are compared at most once, and only when one of them is the pivot.
- They stay in the same range until a pivot is picked from zᵢ … zⱼ. If that first pivot is zᵢ or zⱼ, the two are compared. If it falls strictly between them, they are separated and never compared.
- Each of the j − i + 1 candidates is equally likely to be that first pivot, so P(compared) = 2 / (j − i + 1).
Summing over all pairs gives about 2n ln n ≈ 1.39 n log₂ n expected comparisons, which is Θ(n log n). The constant is only about 39% above the best case.
Seeing it in numbers
import sys
sys.setrecursionlimit(5000)
import random
count = 0
def lomuto_partition(a, lo, hi):
global count
pivot = a[hi]
i = lo
for j in range(lo, hi):
count += 1
if a[j] < pivot:
a[i], a[j] = a[j], a[i]
i += 1
a[i], a[hi] = a[hi], a[i]
return i
def quicksort(a, lo, hi):
if lo < hi:
p = lomuto_partition(a, lo, hi)
quicksort(a, lo, p - 1)
quicksort(a, p + 1, hi)
n = 1000
for name, data in [("random", random.sample(range(n), n)), ("sorted", list(range(n)))]:
count = 0
quicksort(data, 0, n - 1)
print(name, count)
# random: roughly 11,000 (varies per run)
# sorted: 499500 = n(n-1)/2Summary
| Case | Split pattern | Recurrence | Comparisons | Stack depth |
|---|---|---|---|---|
| Best | Halves | T(n) = 2T(n/2) + n | ~n log₂ n | log₂ n |
| Average | Random pivot rank | Expected over splits | ~1.39 n log₂ n | O(log n) expected |
| Worst | 0 and n − 1 | T(n) = T(n−1) + n | n(n−1)/2 | n |
The next chapter covers which inputs produce the worst case for each pivot rule.
Exercise
Every pivot happens to split its range so that one side gets n/4 elements and the other gets 3n/4. What is the running time, and why?
Show answer
Θ(n log n). Each level of the recursion tree still does at most n total work. The longest path shrinks the range by a factor of 4/3 per level, so the depth is log₄/₃ n, which is Θ(log n). Multiplying the two gives Θ(n log n).
Exercise
Using P(compared) = 2/(j − i + 1), what is the probability that the smallest and largest elements, z₁ and zₙ, are ever compared?
Show answer
2/n. They are compared only if the first pivot chosen from the whole array is z₁ or zₙ.