Algorithm Search⌘K

Proofs: Median of medians

These are the human proofs of Median of medians, at full length.

Median of medians returns the k-th smallest item and stops full

This proof shows: correctness, termination.

For a list SS of nn items and 1≤k≤n1 \le k \le n, write sel⁡(S,k)\operatorname{sel}(S, k) for the kk-th entry of a sorted order of SS. All sorted orders of SS have the same entries, so this value is well defined.

Claim. For each list SS of n≥1n \ge 1 items and each kk with 1≤k≤n1 \le k \le n, SELECT(S,k)(S, k) stops and returns sel⁡(S,k)\operatorname{sel}(S, k).

Lemma (split). Let xx be an item, and let LL, EE and GG be the items of SS that are less than, equal to and greater than xx. Then

sel⁡(S,k)={sel⁡(L,k)if k≤∣L∣,xif ∣L∣<k≤∣L∣+∣E∣,sel⁡(G,k−∣L∣−∣E∣)if k>∣L∣+∣E∣.\operatorname{sel}(S, k) = \begin{cases} \operatorname{sel}(L, k) & \text{if } k \le |L|, \\ x & \text{if } |L| < k \le |L| + |E|, \\ \operatorname{sel}(G, k - |L| - |E|) & \text{if } k > |L| + |E|. \end{cases}

Proof. The order is total, so each item of SS is in exactly one of the three lists. Sort LL and GG, and write EE as ∣E∣|E| copies of xx. The list "sorted LL, then EE, then sorted GG" is sorted: each item of LL is less than xx, and each item of GG is greater than xx. It holds the items of SS, so it is a sorted order of SS. Its entries 11 to ∣L∣|L| are the sorted LL, its entries ∣L∣+1|L| + 1 to ∣L∣+∣E∣|L| + |E| are equal to xx, and its entry ∣L∣+∣E∣+j|L| + |E| + j is the entry jj of the sorted GG. □\square

Proof of the claim. By strong induction on nn. The hypothesis H(m)H(m) says that SELECT(S′,k′)(S', k') stops and returns sel⁡(S′,k′)\operatorname{sel}(S', k') for each list S′S' of mm items and each 1≤k′≤m1 \le k' \le m.

  1. Base, n≤5n \le 5. Line L1 sorts SS by insertion sort, which stops and is correct, and returns its entry of rank kk, which is sel⁡(S,k)\operatorname{sel}(S, k) by definition.

  2. Step, n≥6n \ge 6. Assume H(m)H(m) for each m<nm < n.

    • Line L2 makes g=⌈n/5⌉g = \lceil n/5 \rceil groups. Each group is not empty, and g≤(n+4)/5<ng \le (n + 4)/5 < n because n≥2n \ge 2.
    • Line L3 makes the list MM of gg group medians. Each is an item of SS.
    • Line L4 calls SELECT(M,⌈g/2⌉)(M, \lceil g/2 \rceil). Here 1≤⌈g/2⌉≤g1 \le \lceil g/2 \rceil \le g and g<ng < n, so by H(g)H(g) the call stops and returns x=sel⁡(M,⌈g/2⌉)x = \operatorname{sel}(M, \lceil g/2 \rceil), which is an item of MM and so of SS.
    • Line L5 makes LL, EE and GG as in the lemma. Since xx is an item of SS, ∣E∣≥1|E| \ge 1, so ∣L∣≤n−1|L| \le n - 1 and ∣G∣≤n−1|G| \le n - 1.
    • If k≤∣L∣k \le |L|, line L6 calls SELECT(L,k)(L, k) with 1≤k≤∣L∣<n1 \le k \le |L| < n. By H(∣L∣)H(|L|) it returns sel⁡(L,k)\operatorname{sel}(L, k), which is sel⁡(S,k)\operatorname{sel}(S, k) by the lemma.
    • If ∣L∣<k≤∣L∣+∣E∣|L| < k \le |L| + |E|, line L7 returns xx, which is sel⁡(S,k)\operatorname{sel}(S, k) by the lemma.
    • Otherwise ∣L∣+∣E∣<k≤n|L| + |E| < k \le n, so k′=k−∣L∣−∣E∣k' = k - |L| - |E| satisfies 1≤k′≤n−∣L∣−∣E∣=∣G∣<n1 \le k' \le n - |L| - |E| = |G| < n. Line L8 calls SELECT(G,k′)(G, k'), which by H(∣G∣)H(|G|) returns sel⁡(G,k′)\operatorname{sel}(G, k'), which is sel⁡(S,k)\operatorname{sel}(S, k) by the lemma.

    In each case the call stops, because each recursive call stops, and returns sel⁡(S,k)\operatorname{sel}(S, k).

So H(n)H(n) holds for each n≥1n \ge 1. The proof uses no property of xx except that it is an item of SS. The choice of xx as the median of the medians matters only for the running time, which the Complexity section of the page proves. ■\blacksquare

Back to Median of medians