Algorithm Search⌘K

Proofs: Radix select

These are the human proofs of Radix select, at full length.

Radix select returns the k-th smallest key and stops full

This proof shows: correctness, termination.

Let the keys be integers in [0,2w)[0, 2^w), let d=⌈w/r⌉d = \lceil w / r \rceil, and write dig⁡j(x)=⌊x/2rj⌋ mod 2r\operatorname{dig}_j(x) = \lfloor x / 2^{rj} \rfloor \bmod 2^r for 0≤j<d0 \le j < d. Each key is x=∑j=0d−1dig⁡j(x) 2rjx = \sum_{j=0}^{d-1} \operatorname{dig}_j(x) \, 2^{rj}, because x<2w≤2rdx < 2^w \le 2^{rd}. For a list SS and 1≤k≤∣S∣1 \le k \le |S|, write sel⁡(S,k)\operatorname{sel}(S, k) for the kk-th entry of the sorted order of SS. The claim is: for 1≤k≤n1 \le k \le n, RADIX-SELECT(A,k)(A, k) stops and returns sel⁡(A,k)\operatorname{sel}(A, k).

Termination. The loop of line L2 runs at most dd passes, and each pass is a finite number of steps: line L5 reads ∣S∣|S| keys, and line L7 stops, as shown below, before bb passes 2r−12^r - 1.

Lemma (order by digits). Let xx and yy have the same digits dig⁡i\operatorname{dig}_i for all i>ji > j. If dig⁡j(x)<dig⁡j(y)\operatorname{dig}_j(x) < \operatorname{dig}_j(y) then x<yx < y.

Proof. The common higher digits contribute the same amount HH to both. Then x≤H+dig⁡j(x)2rj+(2rj−1)x \le H + \operatorname{dig}_j(x) 2^{rj} + (2^{rj} - 1), since the lower digits add at most ∑i<j(2r−1)2ri=2rj−1\sum_{i < j} (2^r - 1) 2^{ri} = 2^{rj} - 1, and y≥H+dig⁡j(y)2rj≥H+(dig⁡j(x)+1)2rj>xy \ge H + \operatorname{dig}_j(y) 2^{rj} \ge H + (\operatorname{dig}_j(x) + 1) 2^{rj} > x. □\square

Invariant. At the test of line L3 in the pass for digit jj:

(R) 1≤k≤∣S∣1 \le k \le |S|; all keys of SS have the same digits dig⁡i\operatorname{dig}_i for each i>ji > j; and sel⁡(S,k)=sel⁡(A,k0)\operatorname{sel}(S, k) = \operatorname{sel}(A, k_0), where k0k_0 is the rank given as input.

  1. Initialization. Before the first pass, j=d−1j = d - 1, S=AS = A and k=k0k = k_0. There are no digits above d−1d - 1, so (R) holds.
  2. Maintenance. Assume (R) at the pass for jj, and ∣S∣>1|S| > 1. After line L5, C[c]C[c] is the number of keys of SS with digit jj equal to cc, and ∑cC[c]=∣S∣≥k\sum_c C[c] = |S| \ge k. Line L7 keeps k≥1k \ge 1 and stops at the least bb with k≤C[b]k \le C[b]; such a bb exists because the sums C[0]+⋯+C[c]C[0] + \cdots + C[c] reach ∣S∣|S|. Write k′k' for the value of kk after line L7 and c0=C[0]+⋯+C[b−1]c_0 = C[0] + \cdots + C[b-1]; then k′=k−c0k' = k - c_0 and 1≤k′≤C[b]1 \le k' \le C[b]. Let BB be the list of line L8. By (R) and the lemma, each key of SS with digit jj less than bb is less than each key of BB, and each key with digit jj greater than bb is greater. So the sorted order of SS is the c0c_0 keys of the lower buckets, then the sorted BB, then the rest, and sel⁡(S,k)=sel⁡(B,k−c0)=sel⁡(B,k′)\operatorname{sel}(S, k) = \operatorname{sel}(B, k - c_0) = \operatorname{sel}(B, k'). All keys of BB agree on the digits above jj and on digit jj. So (R) holds for the next pass, with j−1j - 1, S=BS = B and k=k′k = k'.

Conclusion. If line L3 returns, then ∣S∣=1|S| = 1, so k=1k = 1 by (R), and S[1]=sel⁡(S,1)=sel⁡(A,k0)S[1] = \operatorname{sel}(S, 1) = \operatorname{sel}(A, k_0). Otherwise the loop ends after the pass for j=0j = 0; by the maintenance step, the keys of SS then agree on all digits dig⁡0,…,dig⁡d−1\operatorname{dig}_0, \ldots, \operatorname{dig}_{d-1}, so they are all equal, and 1≤k≤∣S∣1 \le k \le |S|. Hence S[1]=sel⁡(S,k)=sel⁡(A,k0)S[1] = \operatorname{sel}(S, k) = \operatorname{sel}(A, k_0), which line L9 returns. ■\blacksquare

Back to Radix select