The Markov–Chebyshev–Chernoff ladder of tail inequalities (one idea at three strengths), used to prove QuickSort is O(n log n) w.h.p. and R-Select finds the median in 2n + o(n).
Last updated 27 June 2026
Lecture 2 — Analysis of Randomized Algorithms; Sorting & Searching
Lecture 1 was a gallery — many algorithms, each analyzed with whatever
trick fit. This lecture stops to build the standard toolbox that every
later analysis reuses, and then spends it on two case studies.
It has two halves:
The probability toolkit — three tail inequalities (Markov, Chebyshev,
Chernoff) that answer the recurring question “how unlikely is it that a
random quantity strays far from its average?” This is the property called
concentration (koncentrácia).
Sorting & searching — two payoffs:
Randomized QuickSort runs in O(nlogn) not just on average but
with high probability — a much stronger guarantee than Lecture 1’s
expectation bound.
Randomized Selection (R-Select) finds the median (or any rank) in
2n+o(n) comparisons — linear time, beating the nlogn of sorting.
The spine of the whole lecture is one idea: the three inequalities form a
ladder. Each rung assumes more about the random variable and pays you back
with a sharper bound. Get that ladder straight and everything else follows.
1. The probability vocabulary (recap, with the right names)
A quick refresher of the objects we keep using. Let X be a random
variable (náhodná premenná), and let Ei be the event “X=i”.
Expected value (stredná hodnota), also written μ:
E[X]=i∑i⋅Pr[X=i].
Variance (rozptyl) and standard deviation (odchýlka) σ:
Var(X)=i∑(i−E[X])2Pr[X=i]=E[(X−E[X])2],σ=Var(X).
Variance measures how spread outX is around its mean.
Union bound (the most-used inequality in the course):
Pr[i⋃Ei]≤i∑Pr[Ei].When is it an equality? Exactly when the events are pairwise disjoint
(no overlap is double-counted).
Linearity of expectation — the engine of Lecture 1, holds with or
without independence:
E[i∑aiXi]=i∑aiE[Xi].
Independence and what it unlocks
X,Y are independent (nezávislé) if
Pr[X=x,Y=y]=Pr[X=x]Pr[Y=y]⟺Pr[X=x∣Y=y]=Pr[X=x].
(k-independence is the same statement for every k-tuple — this is the
notion Lecture 5’s derandomization will weaken on purpose.)
Independence buys two things linearity does not:
E[XY]=E[X]E[Y],Var[X+Y]=Var[X]+Var[Y].
Watch the asymmetry. Expectation adds for free (linearity, always).
Variance adds only under independence. That single fact is why the whole
Chebyshev machinery below needs independence and the linearity arguments of
Lecture 1 did not.
Two estimates we will reach for constantly
1+x≤ex(∀x),(1−n1)n≤e1≤(1−n1)n−1.
The first (1+x≤ex, equivalently 1+y<ey) is the workhorse — it is
exactly the step that turns a product into a clean exponential in the Chernoff
proof.
“With high probability” (s vysokou pravdepodobnosťou, s.v.p.)
An event happens with high probability if its probability is
1−O(nc1) for some constant c>0.
This is the target we aim for. The exponent c matters: if we later want to
union-bound over n bad events (e.g. n root-to-leaf paths in a sort
tree), each must fail with probability ≤1/n2 so the total failure is
≤n⋅1/n2=1/n — still high probability. Keep that bookkeeping in
mind; it dictates how sharp a tail bound we need.
2. The running example — balls into boxes (guličky do krabíc)
To compare the three inequalities we keep one concrete question in front of us:
Throw m balls independently and uniformly into n boxes. Let X = the
number of balls in one fixed box. Then E[X]=m/n.
How likely is that box to be much fuller than average?
X is binomial: X∼Bin(m,1/n), so
E[X]=nm=μ,Var(X)=m⋅n1(1−n1)=μ(1−n1)<μ.
We will ask the same question — what is Pr[X≥2μ]? — of all three
inequalities and watch the answer get sharper each time.
3. Markov’s inequality — the root of everything
Theorem (Markov). If X≥0 (takes only non-negative values), then for
every k>0Pr[X≥k]≤kE[X],equivalentlyPr[X≥kE[X]]≤k1.
Proof — one line of bookkeeping. Throw away every term below k and
underestimate the rest by k:
E[X]=i∑iPr[X=i]≥i≥k∑kPr[X=i]=ki≥k∑Pr[X=i]=kPr[X≥k].
Divide by k. ■
That’s the whole thing. Markov knows only the mean and only that X
can’t go negative. With so little information it can’t say much — but it is
the seed from which the other two grow.
On the balls:Pr[X≥2μ]≤2μμ=21. Almost
useless, but honest.
The deep point to say aloud. Markov is the tail inequality. Chebyshev
and Chernoff are not new ideas — they are Markov applied to a cleverly
transformed version of X. Keep that in mind through the next two sections.
4. Chebyshev’s inequality — Markov on the squared deviation
Markov is weak because it ignores the shape of X. Chebyshev feeds it one
more number — the variance — and in return gets a two-sided bound that
needs no non-negativity assumption.
Theorem (Chebyshev). For any random variable X and every k>0,
Pr[∣X−E[X]∣≥k]≤k2Var(X),equivalentlyPr[∣X−E[X]∣≥kσ]≤k21.
The second form is the memorable one: the probability of being k standard
deviations off the mean is at most 1/k2.
Proof — it really is just Markov. The deviation can be negative, so square
it to make it non-negative, then apply Markov to Y=(X−E[X])2:
Pr[∣X−E[X]∣≥k]=Pr[(X−E[X])2≥k2]≤k2E[Y]=k2Var(X).
The last equality is just the definition E[(X−E[X])2]=Var(X). ■
On the balls. Use Var(X)=μ(1−1/n)<μ:
Pr[X≥2μ]≤Pr[∣X−μ∣≥μ]≤μ2Var(X)<μ2μ=μ1=mn.
Sharper already. Markov gave 21 no matter what; Chebyshev gives
n/m, which is tiny when there are many balls per box (m≫n). The
price was one extra assumption — that we know (and can bound) the variance.
5. Chernoff bound — Markov on the exponential
For a sum of independent indicators we can do dramatically better.
Knowing the tail of such a sum exactly is hopeless — the exact formula is a
sum over all large subsets,
Pr[X≥k]=A⊆{1,…,n}∣A∣≥k∑i∈A∏pii∈/A∏(1−pi),
which has exponentially many terms. Chernoff replaces that monster with a clean
exponentially small bound.
Theorem (Chernoff). Let X1,…,Xn be independent0/1 variables
with pi=Pr[Xi=1], let X=∑iXi and μ=E[X]=∑ipi.
Then:
∀δ>0:Pr[X≥(1+δ)μ]<((1+δ)1+δeδ)μ(⋆)∀δ∈(0,1):Pr[X≥(1+δ)μ]≤e−μδ2/3∀δ∈(0,1):Pr[X≤(1−δ)μ]≤e−μδ2/2∀R≥6μ:Pr[X≥R]≤2−R
The middle two are the usable everyday forms — “the probability of being a
δ-fraction off the mean decays like e−μδ2.” The key
feature: the bound shrinks exponentially in μ, not polynomially.
The proof — the “MGF + Markov” trick (worth memorizing)
The recipe is three moves: make it non-negative → exponentiate → Markov.
Step 1 — exponentiate, then Markov.etX is non-negative for any t>0,
and x↦etx is increasing, so the event X≥(1+δ)μ is the
same event as etX≥et(1+δ)μ. Apply Markov to etX:
Pr[X≥(1+δ)μ]=Pr[etX≥et(1+δ)μ]≤et(1+δ)μE[etX].
Step 2 — the moment generating function factorizes (here is where
independence is spent):
E[etX]=E[et∑iXi]=E[i∏etXi]=indep.i∏E[etXi]=i∏(piet+(1−pi)).
Now use 1+y<ey with y=pi(et−1):
i∏(1+pi(et−1))<i∏epi(et−1)=e(et−1)∑ipi=eμ(et−1).
Step 3 — optimize t. Substituting back,
Pr[X≥(1+δ)μ]<et(1+δ)μeμ(et−1)=e(et−1−t(1+δ))μ.
Minimize the exponent over t: the derivative gives et=1+δ, i.e.
t=ln(1+δ). Plugging in yields exactly (⋆):
Pr[X≥(1+δ)μ]<((1+δ)1+δeδ)μ.■
The friendlier forms (e−μδ2/3 etc.) come from bounding this
expression for δ in the stated ranges.
On the balls. With μ=m/n and δ=1:
Pr[X≥2μ]≤e−μδ2/3=e−m/(3n).
The escalation, side by side (same event, Pr[X≥2μ] for the
fullest box):
Markov21≫Chebyshevmn≫Chernoffe−m/(3n).
Constant → polynomially small → exponentially small. Each rung cost one
more assumption.
6. The ladder (the unifying idea — say this in the oral)
The three inequalities are one idea at three resolutions. Read the table
top to bottom: each row assumes strictly more and pays back a strictly sharper
tail.
Inequality
Needs
Mechanism
Tail decay
Markov
X≥0, the mean
— (direct)
∼1/k
Chebyshev
the variance
Markov on (X−μ)2
∼1/k2
Chernoff
independence of a sum
Markov on etX
∼e−k
The one sentence that ties it together.Chebyshev and Chernoff are both
Markov in disguise — applied not to X but to a transformed variable that
amplifies the tail before Markov sees it. Squaring ((X−μ)2) turns a
two-sided question into a one-sided non-negative one and earns a 1/k2.
Exponentiating (etX) is even more aggressive: it blows the tail up so
violently that, after optimizing the knob t, what survives decays
exponentially. Stronger transform → stronger bound — but etX only
factorizes when the terms are independent, which is the price Chernoff
pays and the other two don’t.
7. RQS is O(nlogn)with high probability
Lecture 1 proved randomized QuickSort uses ≈2nlnn comparisons in
expectation. That is an average — a single run could (rarely) be much worse.
Now we upgrade to a with-high-probability guarantee: almost every run is
O(nlogn), not just the average run. Chernoff is exactly the tool for this.
Good pivots vs. bad pivots
Model a run as the recursion treeRQS(S): root S, children
S< and S> (the elements below / above the pivot). Total work is
O(n⋅depth), so it suffices to bound the depth, i.e. the
length of the longest root-to-leaf path.
Call a pivot good if it splits its set in a roughly balanced way:
∣S<∣,∣S>∣≤32∣S∣.
A pivot is good iff it lands in the middle third of the sorted order, so
Pr[pivot is good]=31,Pr[bad]=32.
Why good pivots cap the depth. Each good pivot shrinks the set by a factor
≤32. If S,S1,S2,… are the sets where good pivots occurred
along a path, then ∣Si∣≤(32)i∣S∣. The set hits size 1
after at most
clogn good pivots,c=ln(3/2)1≈2.43
(good pivots on a path ≤clnn). So a path can only be long if it is
stuffed with bad pivots — and bad pivots are where the randomness can be
pinned down by Chernoff.
The Chernoff step
Fix a single root-to-leaf path and look at its first 60lnn vertices. Let
Xi={10i-th vertex on the path has a bad pivototherwise,Pr[Xi=1]=32,
and X=∑i=160lnnXi, so μ=E[X]=32⋅60lnn=40lnn.
A path with more than 60lnn vertices must contain at least
60lnn−2.43lnn=57.57lnn bad ones (only ≤2.43lnn can be good).
So the path being too long forces X far above its mean:
Pr[path>60lnn]≤Pr[X≥57.57lnn]<Pr[X≥56lnn]=Pr[X≥(1+52)μ40lnn].
With δ=52, the Chernoff form Pr[X≥(1+δ)μ]≤e−μδ2/3 gives
≤exp(−40lnn⋅3(2/5)2)=n−160/75<n−2.
From one path to the whole tree (union bound)
One path is short except with probability <n−2. There are at most n
leaves, hence ≤n paths. Union bound:
Pr[some path>60lnn]≤n⋅n−2=n1.
So every path has length O(logn) with probability ≥1−1/n, i.e.
the tree has depth O(logn) w.h.p., i.e. RQS does O(nlogn) work w.h.p.
Punchline. This is strictly stronger than the expectation bound. The
argument is the canonical w.h.p. recipe: (1) isolate “good” events with a
constant success probability; (2) Chernoff a single object to make its
failure ≤n−2; (3) union-bound over the n objects to get total
failure ≤1/n. The exponent-2 in step (2) is engineered precisely so
that step (3) survives — this is why we needed Chernoff and not Chebyshev.
8. R-Select — finding the median in 2n+o(n) comparisons
Sorting finds the median in O(nlogn). Can we do linear? Yes — and the
idea is pure sampling: look at a sublinear random sample, use it to trap the
median inside a tiny window, then sort only that window.
The algorithm (median = the 2n-th element)
Input: set S, ∣S∣=n. Output: the median m.
Sample. Draw R←n3/4 elements from S, uniformly with
replacement.
Bracket. Sort R. Let
ℓ=2n3/4−n,u=2n3/4+n,
and let d,h be the ℓ-th and u-th smallest elements of R. These
two sampled values are our guessed lower/upper fence around the median.
Filter S against the fences:D={x∈S:x<d},H={x∈S:x>h},C={x∈S:d≤x≤h}.
Decide. If ∣D∣>2nor∣H∣>2nor∣C∣>4n3/4, then FAIL. Otherwise the median lies in the small set
C — find it there (by sorting C).
Why 2n+o(n) comparisons. Sorting R costs O(n3/4logn)=o(n).
Building D,H,C compares each of the n elements against d and h —
that’s the 2n. Sorting the surviving window C (size ≤4n3/4) costs
o(n) again. The two fence-tests dominate: 2n+o(n), genuinely linear.
The three FAIL conditions are exactly the three ways the plan can go wrong, and
each is killed by Chebyshev (we don’t even need Chernoff here — we only
need the failure probability to vanish).
Bounding Pr[∣D∣>n/2] — Chebyshev on the sample count
∣D∣>2n means more than half of S lies below the fence d, i.e.
the true median fell belowd (we bracketed too high). Let
Xi={10i-th sampled element≤motherwise,X=i=1∑n3/4Xi.
Since m is the median, each sample lands ≤m with probability exactly
21, so X∼Bin(n3/4,21) with
E[X]=2n3/4,Var(X)=4n3/4.
The bad event “d ended up above m” happens iff fewer than ℓ samples
were ≤m, i.e. X<2n3/4−n. Apply Chebyshev with
k=n:
Pr[X<2n3/4−n]≤Pr[∣X−E[X]∣>n]≤(n)2Var(X)=nn3/4/4=4n−1/4.
By symmetry Pr[∣H∣>2n]≤4n−1/4 as well.
Bounding Pr[∣C∣>4n3/4] — the window stays small
If the window C is too big, then it bulges on one side of the median:
∣C∣>4n3/4⟹∣C≤m∣>2n3/4 or ∣C≥m∣>2n3/4.
Take the upper bulge ∣C≥m∣>2n3/4: it means the fence h sits a
full 2n3/4 ranks above the median, i.e. h is among the top
2n−2n3/4 elements of S. Let
Yi={10i-th sample is among the top 2n−2n3/4 of Sotherwise,Y=i=1∑n3/4Yi,
so Y∼Bin(n3/4,p) with p=21−n1/42:
E[Y]=n3/4p=2n3/4−2n,Var(Y)=n3/4p(1−p)<4n3/4.
The bulge forces Y>2n3/4−n, which is n above
E[Y]. Chebyshev again:
Pr[Y>2n3/4−n]=Pr[Y−E[Y]>n]≤Pr[∣Y−E[Y]∣>n]<4n−1/4.
Two sides, so Pr[∣C∣>4n3/4]<2⋅4n−1/4=2n−1/4.
Putting the three together
Theorem. R-Select finds the median with probability ≥1−n−1/4.
On FAIL, just restart. Failures are independent, so
Pr[FAIL after ℓ runs]≤n−ℓ/4, and the expected
number of runs is E[ℓ]<2. So the expected total cost stays 2n+o(n).
Punchline. A sublinear sample of size n3/4 is enough to pin the
median’s rank to within ±n (the standard-deviation scale of a
binomial), which traps the answer in a window of size O(n3/4) that we can
afford to sort outright. Chebyshev — the middle rung of the ladder — is
already strong enough, because we only need failure →0, not n−c.
This 2n+o(n) is essentially the optimal comparison constant for
selection.
Generalization — the k-th smallest element
The same template returns the k-th smallest Sk for any rank k (not just
the median). Only the centering changes: set x=k/n1/4 and put the sample
fences at ℓ=max{⌊x−n⌋,1} and
u=min{⌈x+n⌉,n3/4}. Near the ends (k<n1/4 or
k>n−n1/4) you keep a one-sided window {x≤h} resp.
{x≥d}; in the bulk you keep {d≤x≤h}. FAIL if the window
grows past the O(n3/4) budget. Same guarantee: Pr[FAIL]≤n−1/4,
cost 2n+o(n).
Recurring themes to carry forward
Theme
Where it appeared
Reused later in
The tail-bound ladder (Markov ⊂ Chebyshev ⊂ Chernoff)
§3–6
every concentration argument in the course
“Markov on a transformed variable” (square → Chebyshev, exponentiate → Chernoff)
§4, §5
the meta-trick to remember
MGF + optimize t (the Chernoff proof)
§5
error amplification, expander walks
Chernoff + union bound → w.h.p. (engineer n−2 per object, sum over n)
§7 RQS depth
any “every one of n things behaves” claim
Variance adds only under independence
§1
why Chebyshev needs independent summands
Sample to estimate ranks (sublinear sample fixes the answer to ±n)
§8 R-Select
sampling-based algorithms generally
Restart-on-FAIL (E[ℓ]<2)
§8
turning a Monte-Carlo failure into Las-Vegas expected cost
The single sentence that ties the lecture together:
Concentration is the whole game: a random quantity almost never strays far
from its mean, and the three inequalities are one idea — apply Markov after a
transform — at escalating strength. Spend a sharper bound where you must
(Chernoff to union-bound RQS over n paths) and a cheaper one where you can
(Chebyshev to keep R-Select’s sample window small).