Algorithm Search⌘K

Proofs: Floyd-Rivest selection

These are the human proofs of Floyd-Rivest selection, at full length.

Floyd-Rivest selection 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 have the same entries, so it is well defined.

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

Lemma (split by two pivots). Let u≤vu \le v be items, and let LL, MM and GG be the items of SS that are less than uu, in [u,v][u, v], and greater than vv. Then

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

Proof. The order is total, so each item is in exactly one part. Each item of LL is less than uu, which is at most each item of MM, and each item of MM is at most vv, which is less than each item of GG. So the list "sorted LL, sorted MM, sorted GG" is a sorted order of SS, and its entry kk is the entry named in the case. □\square

Lines L7 and L8 build these parts. In line L7, an item goes to GG if x>vx > v; otherwise x≤vx \le v, and it goes to LL if x<ux < u, and to MM if u≤x≤vu \le x \le v. Line L8 tests x<ux < u first and x>vx > v second; since u≤vu \le v, an item with x<ux < u is not greater than vv, so again each item goes to the part the lemma names.

Proof of the claim. By strong induction on nn. Assume the claim for all lists with fewer than nn items.

  1. Line L1. If n≤cn \le c, sorting returns sel⁡(S,k)\operatorname{sel}(S, k) by definition.
  2. Lines L2 to L5. Now n>c≥2n > c \ge 2, so m=min⁡(n−1,⌈n2/3⌉)m = \min(n - 1, \lceil n^{2/3} \rceil) satisfies 1≤m≤n−11 \le m \le n - 1, and RR is a list of mm items of SS. Line L4 gives 1≤lo≤hi≤m1 \le lo \le hi \le m: the lower end is at least 11 by the max, the upper end is at most mm by the min, and ⌊km/n−g⌋≤⌈km/n+g⌉\lfloor km/n - g \rfloor \le \lceil km/n + g \rceil with 1≤⌈km/n+g⌉1 \le \lceil km/n + g \rceil since km/n>0km/n > 0, and ⌊km/n−g⌋≤m\lfloor km/n - g \rfloor \le m since k≤nk \le n. By the hypothesis for m<nm < n, line L5 returns u=sel⁡(R,lo)u = \operatorname{sel}(R, lo) and v=sel⁡(R,hi)v = \operatorname{sel}(R, hi), and u≤vu \le v because lo≤hilo \le hi. Both are items of SS.
  3. Lines L9 and L10. uu is in SS and is not less than itself, so u∉Lu \notin L and ∣L∣≤n−1|L| \le n - 1; likewise v∉Gv \notin G and ∣G∣≤n−1|G| \le n - 1. If k≤∣L∣k \le |L|, the call of line L9 has 1≤k≤∣L∣<n1 \le k \le |L| < n, and by the hypothesis and the lemma it returns sel⁡(S,k)\operatorname{sel}(S, k). If k>∣L∣+∣M∣k > |L| + |M|, then 1≤k−∣L∣−∣M∣≤n−∣L∣−∣M∣=∣G∣<n1 \le k - |L| - |M| \le n - |L| - |M| = |G| < n, and the same argument applies to line L10.
  4. Lines L11 and L12. Otherwise ∣L∣<k≤∣L∣+∣M∣|L| < k \le |L| + |M|, so 1≤k−∣L∣≤∣M∣1 \le k - |L| \le |M|. If ∣M∣=n|M| = n, line L11 returns sel⁡(S,k)\operatorname{sel}(S, k) by sorting. If ∣M∣<n|M| < n, line L12 calls FR-SELECT(M,k−∣L∣)(M, k - |L|) on a shorter list, which by the hypothesis returns sel⁡(M,k−∣L∣)\operatorname{sel}(M, k - |L|), which is sel⁡(S,k)\operatorname{sel}(S, k) by the lemma.

Each case either returns directly or makes recursive calls on lists with fewer than nn items, each of which stops by the hypothesis, so the call stops and returns sel⁡(S,k)\operatorname{sel}(S, k). The proof uses no property of the sample except that RR is a list of mm items of SS, so it holds for every outcome of the draws. ■\blacksquare

Back to Floyd-Rivest selection