QUICKSORT(A, lo, hi):
if lo < hi:
r = RANDOM(lo, hi) # <-- the only new line
swap A[r] with A[hi] # move random pivot to the end
p = PARTITION(A, lo, hi) # Lomuto/Hoare around A[hi]
QUICKSORT(A, lo, p-1)
QUICKSORT(A, p+1, hi)
Why this step? Every element in Zij has the same chance of being picked first as a pivot — that's the whole payoff of uniform randomization. Two favorable choices out of j−i+1 → probability 2/(j−i+1).
When are two elements ever compared in quicksort? → only when one is the pivot.
Why is Pr[zi↔zj]=j−i+12? → first pivot in Zij decides; 2 good out of j−i+1.
Why does randomization beat the adversary? → cost depends on coin flips, not input order.
Worst case of randomized quicksort? → still O(n2) (just improbable).
What does randomization change about quicksort's complexity?
Worst case stays O(n2) but expected time becomes O(nlogn) for every input; no input is adversarially bad.
When are two elements zi,zj compared in quicksort?
Exactly when one of them is chosen as a pivot while both are still in the same subarray — at most once.
What is Pr[zi and zj are compared]?
j−i+12, since the first of the j−i+1 elements between them (inclusive) to be picked as pivot must be zi or zj.
Why is the indicator Xij either 0 or 1?
A pair is compared at most once because the pivot involved is removed from future recursion; opposite-side elements are never compared again.
What is E[X] in closed form?
E[X]=∑i<jj−i+12<2(n−1)Hn=O(nlogn), where Hn=Θ(lnn).
What is the harmonic number Hn asymptotically?
Hn=∑k=1n1/k≈lnn+0.577, so Θ(logn).
Probability two adjacent (in sorted order) elements are compared?
22=1 — adjacent elements are always compared.
Does a single random shuffle at the start work as well as random pivots?
Yes — both make pivot ranks uniformly random, giving the same expected O(nlogn).
Recall Feynman: explain it to a 12-year-old
Imagine sorting a deck of cards by picking one card (the "splitter"), then putting smaller cards left and bigger cards right, and repeating on each pile. If you always pick the smallest card as splitter, your piles are lopsided (1 vs the rest) and it takes forever. The trick: pick the splitter card at random each time. Now there's no sneaky way for someone to arrange the deck to slow you down — because they can't guess which card you'll grab. On average your piles split nicely in half, so you finish in about nlogn steps instead of n2. Two cards only ever get directly compared if one of them happens to be a splitter while they're still in the same pile — and that gives the neat little fraction 2/(gap+1).
Dekho, normal quicksort ka problem yeh hai ki agar pivot hamesha pehla ya aakhri element lo, toh koi smart banda tumhe already sorted array de dega — aur tumhara pivot har baar sabse chhota/bada niklega. Tab ek side mein n−1 elements aur doosri side khaali — recursion depth n ho gaya, time O(n2). Yani input ka order tumhe maar deta hai.
Solution simple hai: har step pe pivot ko randomly uthao (uniformly). Ab badi baat — cost ab input ke order pe depend nahi karta, balki tumhare coin flips pe karta hai. Adversary kitna bhi clever ho, woh tumhare random choices guess nahi kar sakta, toh koi bhi input "bad input" nahi reh jaata. Isliye expected time har input ke liye O(nlogn) ho jaata hai.
Proof ka core idea: do elements zi aur zj (sorted order mein) sirf tab compare hote hain jab unme se ek pivot bane. Unke beech ke j−i+1 elements mein se jo pehla pivot banta hai, wahi decide karta hai — agar woh zi ya zj hua toh compare honge, beech wala koi hua toh alag piles mein chale jaayenge. Toh probability =2/(j−i+1). Sab pairs ka expectation jodo (linearity of expectation), gap substitute karo, aur harmonic series Hn≈lnn aa jaata hai — final answer O(nlogn).
Yaad rakhna ki worst case abhi bhi O(n2) hai — bas uski probability itni kam hai ki practically kabhi nahi aata. Random pivot "bad luck" nahi hataata, "bad input" hataata hai. Yeh technique (indicator variables + linearity) interviews aur exams mein bahut kaam aati hai, toh derivation rata mat, samajh ke yaad rakho.