Main

Lecture 2: Analysis of Randomized Algorithms

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

Course 2-INF-135/15 Pravdepodobnostné algoritmy, LS 2025/26. Source slides: RA_02.pdf (16 pages).

What this lecture is about

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:

  1. 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).
  2. Sorting & searching — two payoffs:
    • Randomized QuickSort runs in O(nlogn)O(n\log n) 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)2n + o(n) comparisons — linear time, beating the nlognn\log n 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 XX be a random variable (náhodná premenná), and let EiE_i be the event “X=iX = i”.

Independence and what it unlocks

X,YX, Y are independent (nezávislé) if

Pr[X=x,Y=y]=Pr[X=x]Pr[Y=y]Pr[X=xY=y]=Pr[X=x].\Pr[X = x, Y = y] = \Pr[X=x]\Pr[Y=y] \quad\Longleftrightarrow\quad \Pr[X=x \mid Y=y] = \Pr[X=x].
(kk-independence is the same statement for every kk-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].E[XY] = E[X]\,E[Y], \qquad \mathrm{Var}[X+Y] = \mathrm{Var}[X] + \mathrm{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+xex(x),(11n)n1e(11n)n1.1 + x \le e^x \quad(\forall x), \qquad\qquad \left(1 - \tfrac1n\right)^{n} \le \tfrac1e \le \left(1 - \tfrac1n\right)^{n-1}.

The first (1+xex1+x \le e^x, equivalently 1+y<ey1+y < e^y) 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 1O ⁣(1nc)1 - O\!\left(\dfrac{1}{n^c}\right) for some constant c>0c > 0.

This is the target we aim for. The exponent cc matters: if we later want to union-bound over nn bad events (e.g. nn root-to-leaf paths in a sort tree), each must fail with probability 1/n2\le 1/n^2 so the total failure is n1/n2=1/n\le n \cdot 1/n^2 = 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 mm balls independently and uniformly into nn boxes. Let XX = the number of balls in one fixed box. Then E[X]=m/nE[X] = m/n. How likely is that box to be much fuller than average?

XX is binomial: XBin(m,1/n)X \sim \mathrm{Bin}(m, 1/n), so

E[X]=mn=μ,Var(X)=m1n(11n)=μ(11n)<μ.E[X] = \tfrac{m}{n} = \mu, \qquad \mathrm{Var}(X) = m\cdot\tfrac1n\Big(1-\tfrac1n\Big) = \mu\Big(1-\tfrac1n\Big) < \mu.

We will ask the same question — what is Pr[X2μ]\Pr[X \ge 2\mu]? — of all three inequalities and watch the answer get sharper each time.


3. Markov’s inequality — the root of everything

Theorem (Markov). If X0X \ge 0 (takes only non-negative values), then for every k>0k > 0

Pr[Xk]E[X]k,equivalentlyPr[XkE[X]]1k.\Pr[X \ge k] \le \frac{E[X]}{k}, \qquad\text{equivalently}\qquad \Pr[X \ge k\,E[X]] \le \frac1k.

Proof — one line of bookkeeping. Throw away every term below kk and underestimate the rest by kk:

E[X]=iiPr[X=i]    ikkPr[X=i]=kikPr[X=i]=kPr[Xk].E[X] = \sum_i i\,\Pr[X=i] \;\ge\; \sum_{i \ge k} k\,\Pr[X=i] = k\sum_{i\ge k}\Pr[X=i] = k\,\Pr[X\ge k].
Divide by kk. \blacksquare

That’s the whole thing. Markov knows only the mean and only that XX 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[X2μ]μ2μ=12\Pr[X \ge 2\mu] \le \dfrac{\mu}{2\mu} = \dfrac12. 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 XX. 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 XX. 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 XX and every k>0k > 0,

Pr[XE[X]k]Var(X)k2,equivalentlyPr[XE[X]kσ]1k2.\Pr\big[\,|X - E[X]| \ge k\,\big] \le \frac{\mathrm{Var}(X)}{k^2}, \qquad\text{equivalently}\qquad \Pr\big[\,|X - E[X]| \ge k\sigma\,\big] \le \frac{1}{k^2}.

The second form is the memorable one: the probability of being kk standard deviations off the mean is at most 1/k21/k^2.

Proof — it really is just Markov. The deviation can be negative, so square it to make it non-negative, then apply Markov to Y=(XE[X])2Y = (X - E[X])^2:

Pr[XE[X]k]=Pr[(XE[X])2k2]E[Y]k2=Var(X)k2.\Pr\big[\,|X-E[X]| \ge k\,\big] = \Pr\big[\,(X-E[X])^2 \ge k^2\,\big] \le \frac{E[Y]}{k^2} = \frac{\mathrm{Var}(X)}{k^2}.
The last equality is just the definition E[(XE[X])2]=Var(X)E[(X-E[X])^2] = \mathrm{Var}(X). \blacksquare

On the balls. Use Var(X)=μ(11/n)<μ\mathrm{Var}(X) = \mu(1-1/n) < \mu:

Pr[X2μ]Pr[Xμμ]Var(X)μ2<μμ2=1μ=nm.\Pr[X \ge 2\mu] \le \Pr\big[\,|X-\mu| \ge \mu\,\big] \le \frac{\mathrm{Var}(X)}{\mu^2} < \frac{\mu}{\mu^2} = \frac1\mu = \frac{n}{m}.

Sharper already. Markov gave 12\tfrac12 no matter what; Chebyshev gives n/mn/m, which is tiny when there are many balls per box (mnm \gg 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[Xk]=A{1,,n}Ak  iApiiA(1pi),\Pr[X \ge k] = \sum_{\substack{A \subseteq \{1,\dots,n\}\\ |A| \ge k}} \;\prod_{i \in A} p_i \prod_{i \notin A}(1 - p_i),
which has exponentially many terms. Chernoff replaces that monster with a clean exponentially small bound.

Theorem (Chernoff). Let X1,,XnX_1,\dots,X_n be independent 0/10/1 variables with pi=Pr[Xi=1]p_i = \Pr[X_i = 1], let X=iXiX = \sum_i X_i and μ=E[X]=ipi\mu = E[X] = \sum_i p_i. Then:

δ>0:Pr[X(1+δ)μ]<(eδ(1+δ)1+δ) ⁣μ()\forall \delta > 0:\quad \Pr[X \ge (1+\delta)\mu] < \left(\frac{e^{\delta}}{(1+\delta)^{1+\delta}}\right)^{\!\mu} \quad(\star)
δ(0,1):Pr[X(1+δ)μ]eμδ2/3\forall \delta \in (0,1):\quad \Pr[X \ge (1+\delta)\mu] \le e^{-\mu\delta^2/3}
δ(0,1):Pr[X(1δ)μ]eμδ2/2\forall \delta \in (0,1):\quad \Pr[X \le (1-\delta)\mu] \le e^{-\mu\delta^2/2}
R6μ:Pr[XR]2R\forall R \ge 6\mu:\quad \Pr[X \ge R] \le 2^{-R}

The middle two are the usable everyday forms — “the probability of being a δ\delta-fraction off the mean decays like eμδ2e^{-\mu\delta^2}.” The key feature: the bound shrinks exponentially in μ\mu, 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. etXe^{tX} is non-negative for any t>0t>0, and xetxx \mapsto e^{tx} is increasing, so the event X(1+δ)μX \ge (1+\delta)\mu is the same event as etXet(1+δ)μe^{tX} \ge e^{t(1+\delta)\mu}. Apply Markov to etXe^{tX}:

Pr[X(1+δ)μ]=Pr[etXet(1+δ)μ]E[etX]et(1+δ)μ.\Pr[X \ge (1+\delta)\mu] = \Pr\big[e^{tX} \ge e^{t(1+\delta)\mu}\big] \le \frac{E[e^{tX}]}{e^{t(1+\delta)\mu}}.

Step 2 — the moment generating function factorizes (here is where independence is spent):

E[etX]=E[etiXi]=E[ietXi]=indep.iE[etXi]=i(piet+(1pi)).E[e^{tX}] = E\big[e^{t\sum_i X_i}\big] = E\Big[\prod_i e^{tX_i}\Big] \overset{\text{indep.}}{=} \prod_i E[e^{tX_i}] = \prod_i \big(p_i e^t + (1-p_i)\big).
Now use 1+y<ey1 + y < e^y with y=pi(et1)y = p_i(e^t - 1):
i(1+pi(et1))<iepi(et1)=e(et1)ipi=eμ(et1).\prod_i \big(1 + p_i(e^t - 1)\big) < \prod_i e^{p_i(e^t-1)} = e^{(e^t - 1)\sum_i p_i} = e^{\mu(e^t - 1)}.

Step 3 — optimize tt. Substituting back,

Pr[X(1+δ)μ]<eμ(et1)et(1+δ)μ=e(et1t(1+δ))μ.\Pr[X \ge (1+\delta)\mu] < \frac{e^{\mu(e^t-1)}}{e^{t(1+\delta)\mu}} = e^{\big(e^t - 1 - t(1+\delta)\big)\mu}.
Minimize the exponent over tt: the derivative gives et=1+δe^t = 1+\delta, i.e. t=ln(1+δ)t = \ln(1+\delta). Plugging in yields exactly ()(\star):
Pr[X(1+δ)μ]<(eδ(1+δ)1+δ) ⁣μ.\Pr[X \ge (1+\delta)\mu] < \left(\frac{e^\delta}{(1+\delta)^{1+\delta}}\right)^{\!\mu}. \qquad\blacksquare
The friendlier forms (eμδ2/3e^{-\mu\delta^2/3} etc.) come from bounding this expression for δ\delta in the stated ranges.

On the balls. With μ=m/n\mu = m/n and δ=1\delta = 1:

Pr[X2μ]eμδ2/3=em/(3n).\Pr[X \ge 2\mu] \le e^{-\mu\delta^2/3} = e^{-m/(3n)}.

The escalation, side by side (same event, Pr[X2μ]\Pr[X \ge 2\mu] for the fullest box):

12Markov        nmChebyshev        em/(3n)Chernoff.\underbrace{\tfrac12}_{\text{Markov}} \;\;\gg\;\; \underbrace{\tfrac{n}{m}}_{\text{Chebyshev}} \;\;\gg\;\; \underbrace{e^{-m/(3n)}}_{\text{Chernoff}}.
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 X0X \ge 0, the mean — (direct) 1/k\sim 1/k
Chebyshev the variance Markov on (Xμ)2(X-\mu)^2 1/k2\sim 1/k^2
Chernoff independence of a sum Markov on etXe^{tX} ek\sim e^{-k}

The one sentence that ties it together. Chebyshev and Chernoff are both Markov in disguise — applied not to XX but to a transformed variable that amplifies the tail before Markov sees it. Squaring ((Xμ)2(X-\mu)^2) turns a two-sided question into a one-sided non-negative one and earns a 1/k21/k^2. Exponentiating (etXe^{tX}) is even more aggressive: it blows the tail up so violently that, after optimizing the knob tt, what survives decays exponentially. Stronger transform → stronger bound — but etXe^{tX} only factorizes when the terms are independent, which is the price Chernoff pays and the other two don’t.


7. RQS is O(nlogn)O(n\log n) with high probability

Lecture 1 proved randomized QuickSort uses 2nlnn\approx 2n\ln n 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)O(n\log n), not just the average run. Chernoff is exactly the tool for this.

Good pivots vs. bad pivots

Model a run as the recursion tree RQS(S)\mathrm{RQS}(S): root SS, children S<S_{<} and S>S_{>} (the elements below / above the pivot). Total work is O(ndepth)O(n \cdot \text{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>23S.|S_{<}|,\ |S_{>}| \le \tfrac{2}{3}|S|.

A pivot is good iff it lands in the middle third of the sorted order, so

Pr[pivot is good]=13,Pr[bad]=23.\Pr[\text{pivot is good}] = \tfrac13, \qquad \Pr[\text{bad}] = \tfrac23.

Why good pivots cap the depth. Each good pivot shrinks the set by a factor 23\le \tfrac23. If S,S1,S2,S, S_1, S_2, \dots are the sets where good pivots occurred along a path, then Si(23)iS|S_i| \le \big(\tfrac23\big)^i |S|. The set hits size 11 after at most

clogn good pivots,c=1ln(3/2)2.43c\log n \text{ good pivots}, \qquad c = \frac{1}{\ln(3/2)} \approx 2.43
(good pivots on a path clnn\le c\ln n). 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 60lnn60\ln n vertices. Let

Xi={1i-th vertex on the path has a bad pivot0otherwise,Pr[Xi=1]=23,X_i = \begin{cases}1 & i\text{-th vertex on the path has a }\textbf{bad}\text{ pivot}\\ 0 & \text{otherwise}\end{cases}, \qquad \Pr[X_i = 1] = \tfrac23,
and X=i=160lnnXiX = \sum_{i=1}^{60\ln n} X_i, so μ=E[X]=2360lnn=40lnn\mu = E[X] = \tfrac23 \cdot 60\ln n = 40\ln n.

A path with more than 60lnn60\ln n vertices must contain at least 60lnn2.43lnn=57.57lnn60\ln n - 2.43\ln n = 57.57\ln n bad ones (only 2.43lnn\le 2.43\ln n can be good). So the path being too long forces XX far above its mean:

Pr[path>60lnn]Pr[X57.57lnn]<Pr[X56lnn]=Pr ⁣[X(1+25)40lnnμ].\Pr[\text{path} > 60\ln n] \le \Pr[X \ge 57.57\ln n] < \Pr[X \ge 56\ln n] = \Pr\!\Big[X \ge \big(1 + \tfrac{2}{5}\big)\underbrace{40\ln n}_{\mu}\Big].
With δ=25\delta = \tfrac25, the Chernoff form Pr[X(1+δ)μ]eμδ2/3\Pr[X \ge (1+\delta)\mu] \le e^{-\mu\delta^2/3} gives
exp ⁣(40lnn(2/5)23)=n160/75<n2.\le \exp\!\Big(-40\ln n \cdot \tfrac{(2/5)^2}{3}\Big) = n^{-160/75} < n^{-2}.

From one path to the whole tree (union bound)

One path is short except with probability <n2< n^{-2}. There are at most nn leaves, hence n\le n paths. Union bound:

Pr[some path>60lnn]nn2=1n.\Pr[\text{some path} > 60\ln n] \le n \cdot n^{-2} = \tfrac1n.

So every path has length O(logn)O(\log n) with probability 11/n\ge 1 - 1/n, i.e. the tree has depth O(logn)O(\log n) w.h.p., i.e. RQS does O(nlogn)O(n\log n) 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 n2\le n^{-2}; (3) union-bound over the nn objects to get total failure 1/n\le 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)2n + o(n) comparisons

Sorting finds the median in O(nlogn)O(n\log n). 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 n2\tfrac n2-th element)

Input: set SS, S=n|S| = n. Output: the median mm.

  1. Sample. Draw Rn3/4R \leftarrow n^{3/4} elements from SS, uniformly with replacement.
  2. Bracket. Sort RR. Let
    =n3/42n,u=n3/42+n,\ell = \tfrac{n^{3/4}}{2} - \sqrt n, \qquad u = \tfrac{n^{3/4}}{2} + \sqrt n,
    and let d,hd, h be the \ell-th and uu-th smallest elements of RR. These two sampled values are our guessed lower/upper fence around the median.
  3. Filter SS against the fences:
    D={xS:x<d},H={xS:x>h},C={xS:dxh}.D = \{x \in S : x < d\}, \quad H = \{x \in S : x > h\}, \quad C = \{x \in S : d \le x \le h\}.
  4. Decide. If D>n2|D| > \tfrac n2 or H>n2|H| > \tfrac n2 or C>4n3/4|C| > 4n^{3/4}, then FAIL. Otherwise the median lies in the small set CC — find it there (by sorting CC).

Why 2n+o(n)2n + o(n) comparisons. Sorting RR costs O(n3/4logn)=o(n)O(n^{3/4}\log n) = o(n). Building D,H,CD, H, C compares each of the nn elements against dd and hh — that’s the 2n2n. Sorting the surviving window CC (size 4n3/4\le 4n^{3/4}) costs o(n)o(n) again. The two fence-tests dominate: 2n+o(n)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]\Pr[|D| > n/2] — Chebyshev on the sample count

D>n2|D| > \tfrac n2 means more than half of SS lies below the fence dd, i.e. the true median fell below dd (we bracketed too high). Let

Xi={1i-th sampled elementm0otherwise,X=i=1n3/4Xi.X_i = \begin{cases}1 & i\text{-th sampled element} \le m\\ 0 & \text{otherwise}\end{cases}, \qquad X = \sum_{i=1}^{n^{3/4}} X_i.
Since mm is the median, each sample lands m\le m with probability exactly 12\tfrac12, so XBin(n3/4,12)X \sim \mathrm{Bin}\big(n^{3/4}, \tfrac12\big) with
E[X]=n3/42,Var(X)=n3/44.E[X] = \tfrac{n^{3/4}}{2}, \qquad \mathrm{Var}(X) = \tfrac{n^{3/4}}{4}.
The bad event “dd ended up above mm” happens iff fewer than \ell samples were m\le m, i.e. X<n3/42nX < \tfrac{n^{3/4}}{2} - \sqrt n. Apply Chebyshev with k=nk = \sqrt n:
Pr[X<n3/42n]Pr[XE[X]>n]Var(X)(n)2=n3/4/4n=n1/44.\Pr\Big[X < \tfrac{n^{3/4}}{2} - \sqrt n\Big] \le \Pr\big[|X - E[X]| > \sqrt n\big] \le \frac{\mathrm{Var}(X)}{(\sqrt n)^2} = \frac{n^{3/4}/4}{n} = \frac{n^{-1/4}}{4}.
By symmetry Pr[H>n2]n1/44\Pr[|H| > \tfrac n2] \le \tfrac{n^{-1/4}}{4} as well.

Bounding Pr[C>4n3/4]\Pr[|C| > 4n^{3/4}] — the window stays small

If the window CC is too big, then it bulges on one side of the median:

C>4n3/4    Cm>2n3/4  or  Cm>2n3/4.|C| > 4n^{3/4} \;\Longrightarrow\; |C_{\le m}| > 2n^{3/4} \ \text{ or }\ |C_{\ge m}| > 2n^{3/4}.
Take the upper bulge Cm>2n3/4|C_{\ge m}| > 2n^{3/4}: it means the fence hh sits a full 2n3/42n^{3/4} ranks above the median, i.e. hh is among the top n22n3/4\tfrac n2 - 2n^{3/4} elements of SS. Let
Yi={1i-th sample is among the top n22n3/4 of S0otherwise,Y=i=1n3/4Yi,Y_i = \begin{cases}1 & i\text{-th sample is among the top } \tfrac n2 - 2n^{3/4} \text{ of } S\\ 0 & \text{otherwise}\end{cases}, \qquad Y = \sum_{i=1}^{n^{3/4}} Y_i,
so YBin(n3/4,p)Y \sim \mathrm{Bin}(n^{3/4}, p) with p=122n1/4p = \tfrac12 - \tfrac{2}{n^{1/4}}:
E[Y]=n3/4p=n3/422n,Var(Y)=n3/4p(1p)<n3/44.E[Y] = n^{3/4}p = \tfrac{n^{3/4}}{2} - 2\sqrt n, \qquad \mathrm{Var}(Y) = n^{3/4}\,p(1-p) < \tfrac{n^{3/4}}{4}.
The bulge forces Y>n3/42nY > \tfrac{n^{3/4}}{2} - \sqrt n, which is n\sqrt n above E[Y]E[Y]. Chebyshev again:
Pr[Y>n3/42n]=Pr[YE[Y]>n]Pr[YE[Y]>n]<n1/44.\Pr\Big[Y > \tfrac{n^{3/4}}{2} - \sqrt n\Big] = \Pr\big[Y - E[Y] > \sqrt n\big] \le \Pr\big[|Y - E[Y]| > \sqrt n\big] < \frac{n^{-1/4}}{4}.
Two sides, so Pr[C>4n3/4]<2n1/44=n1/42\Pr[|C| > 4n^{3/4}] < 2\cdot\tfrac{n^{-1/4}}{4} = \tfrac{n^{-1/4}}{2}.

Putting the three together

Theorem. R-Select finds the median with probability 1n1/4\ge 1 - n^{-1/4}.

Pr[FAIL]Pr[D>n2]n1/4/4+Pr[H>n2]n1/4/4+Pr[C>4n3/4]n1/4/2  =  n1/4.\Pr[\text{FAIL}] \le \underbrace{\Pr[|D| > \tfrac n2]}_{\le\, n^{-1/4}/4} + \underbrace{\Pr[|H| > \tfrac n2]}_{\le\, n^{-1/4}/4} + \underbrace{\Pr[|C| > 4n^{3/4}]}_{\le\, n^{-1/4}/2} \;=\; n^{-1/4}.

On FAIL, just restart. Failures are independent, so Pr[FAIL after  runs]n/4\Pr[\text{FAIL after } \ell \text{ runs}] \le n^{-\ell/4}, and the expected number of runs is E[]<2E[\ell] < 2. So the expected total cost stays 2n+o(n)2n + o(n).

Punchline. A sublinear sample of size n3/4n^{3/4} is enough to pin the median’s rank to within ±n\pm\sqrt n (the standard-deviation scale of a binomial), which traps the answer in a window of size O(n3/4)O(n^{3/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\to 0, not ncn^{-c}. This 2n+o(n)2n + o(n) is essentially the optimal comparison constant for selection.

Generalization — the kk-th smallest element

The same template returns the kk-th smallest SkS_k for any rank kk (not just the median). Only the centering changes: set x=k/n1/4x = k/n^{1/4} and put the sample fences at =max{xn,1}\ell = \max\{\lfloor x - \sqrt n\rfloor, 1\} and u=min{x+n,n3/4}u = \min\{\lceil x + \sqrt n\rceil, n^{3/4}\}. Near the ends (k<n1/4k < n^{1/4} or k>nn1/4k > n - n^{1/4}) you keep a one-sided window {xh}\{x \le h\} resp. {xd}\{x \ge d\}; in the bulk you keep {dxh}\{d \le x \le h\}. FAIL if the window grows past the O(n3/4)O(n^{3/4}) budget. Same guarantee: Pr[FAIL]n1/4\Pr[\text{FAIL}] \le n^{-1/4}, cost 2n+o(n)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 tt (the Chernoff proof) §5 error amplification, expander walks
Chernoff + union bound → w.h.p. (engineer n2n^{-2} per object, sum over nn) §7 RQS depth any “every one of nn 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\pm\sqrt n) §8 R-Select sampling-based algorithms generally
Restart-on-FAIL (E[]<2E[\ell] < 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 nn paths) and a cheaper one where you can (Chebyshev to keep R-Select’s sample window small).

concentrationmarkovchebyshevchernoffunion-boundquicksortselection

← Back to Randomized Algorithms