Sub topic 9
Randomized Quicksort
This chapter assumes you know the Lomuto partition scheme, the recursive structure of quicksort, and why a pivot that is always the smallest or largest element leads to O(n²) time. You should also be comfortable with expected value in the basic probability sense.
Moving the Randomness Into the Algorithm
A fixed pivot rule, such as "always take the last element," has bad inputs that are known in advance. Already-sorted data is the classic one. Any rule that is deterministic has some input that defeats it, and anyone who knows the rule can build that input.
Randomized quicksort removes the dependence on input order. Before partitioning, it swaps a uniformly random element of the range into the pivot slot. After that, Lomuto runs unchanged. The running time now depends on the random choices, not on how the input happens to be arranged. The guarantee becomes this: for every input, the expected running time is O(n log n).
import random
def lomuto_partition(a, lo, hi):
pivot = a[hi]
i = lo
for j in range(lo, hi):
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 randomized_partition(a, lo, hi):
r = random.randint(lo, hi)
a[r], a[hi] = a[hi], a[r]
return lomuto_partition(a, lo, hi)
def randomized_quicksort(a, lo=0, hi=None):
if hi is None:
hi = len(a) - 1
if lo < hi:
p = randomized_partition(a, lo, hi)
randomized_quicksort(a, lo, p - 1)
randomized_quicksort(a, p + 1, hi)
data = list(range(20)) # sorted input: worst case for a fixed last pivot
randomized_quicksort(data)
print(data == sorted(data)) # TrueWhy the Expectation Is O(n log n)
Name the elements in sorted order z₁ < z₂ < … < zₙ. Two elements zᵢ and zⱼ are compared at most once, and only when one of them is the pivot. They get compared exactly when the first pivot picked from the range zᵢ…zⱼ is zᵢ or zⱼ. If some element strictly between them is picked first, it sends them to different sides and they never meet. Every element in that range of j − i + 1 values is equally likely to be picked first, so:
P(zᵢ and zⱼ are compared) = 2 / (j − i + 1)
Adding this over all pairs gives at most 2n·Hₙ, where Hₙ is the n-th harmonic number. That makes the expected number of comparisons about 2n ln n ≈ 1.39 n log₂ n, which is O(n log n).
What Randomization Does Not Fix
| Concern | Effect of a random pivot |
|---|---|
| Sorted or reverse-sorted input | Fixed: expected O(n log n) |
| Adversary who knows your pivot rule | Fixed, as long as they cannot predict the random numbers |
| Many equal keys | Not fixed: Lomuto still splits all-equal ranges n−1 / 0 |
| Worst case | Still O(n²), but very unlikely for large n |
| Cost | One random-number call per partition |
Exercise
You run randomized_quicksort on a list of 10,000 copies of the value 7. Why does it still take quadratic time, no matter which index the random choice picks?
Show answer
Every element equals the pivot, so the test a[j] < pivot is never true. Lomuto leaves i at lo, places the pivot at lo, and returns p = lo. One side is empty and the other has n−1 elements every time. The random index makes no difference because all candidates are equal. Three-way partitioning fixes this case.
Exercise
In randomized quicksort on n distinct elements, what is the probability that the smallest element z₁ and the largest element zₙ are ever compared?
Show answer
2/n. The range z₁…zₙ holds all n elements. The two get compared only if the first pivot picked from that range is z₁ or zₙ, which happens with probability 2/n.