Algorithm Search⌘K

Proofs: Introselect

These are the human proofs of Introselect, at full length.

Introselect returns the k-th smallest item and stops full

This proof shows: correctness, termination.

Let SS be the multiset of the input items and s0≤s1≤⋯≤sn−1s_0 \le s_1 \le \cdots \le s_{n-1} its sorted order. The claim is: for 1≤k≤n1 \le k \le n, INTROSELECT(A,k)(A, k) stops and returns sk−1s_{k-1}. We use two facts proved on their own pages: MOM-SELECT(L,r)(L, r) stops and returns the item of rank rr of a list LL (median of medians), and PARTITION3(B,lo,hi,x)(B, lo, hi, x) permutes B[lo…hi−1]B[lo \ldots hi-1], changes no other entry, and returns (p,q)(p, q) with lo≤p≤q≤hilo \le p \le q \le hi, each item of B[lo…p−1]B[lo \ldots p-1] less than xx, each item of B[p…q−1]B[p \ldots q-1] equal to xx, and each item of B[q…hi−1]B[q \ldots hi-1] greater than xx (the three-way template of partition around a pivot).

Invariant. At each test of line L2:

(I) BB is a permutation of SS; lo≤t<hilo \le t < hi; each item of B[0…lo−1]B[0 \ldots lo-1] is at most each item of B[lo…hi−1]B[lo \ldots hi-1]; and each item of B[lo…hi−1]B[lo \ldots hi-1] is at most each item of B[hi…n−1]B[hi \ldots n-1].

Consequence. Under (I), a sorted order of BB is: a sorted order of B[0…lo−1]B[0 \ldots lo-1], then the sorted block DD of B[lo…hi−1]B[lo \ldots hi-1], then a sorted order of B[hi…n−1]B[hi \ldots n-1]. So st=D[t−lo]s_t = D[t - lo], the item of rank t−lo+1t - lo + 1 of the block.

  1. Initialization. Line L1 sets BB to a copy of AA, lo=0lo = 0, hi=nhi = n and t=k−1t = k - 1, so 0≤t<n0 \le t < n and the outer parts are empty.
  2. Maintenance. Assume (I) and hi−lo>1hi - lo > 1, and that lines L3 and L6 do not return. Line L4 picks xx among three items of the block, so xx is an item of the block. Line L5 gives (p,q)(p, q) as above, and since xx is in the block, p<qp < q. Line L6 did not return, so t<pt < p or t≥qt \ge q. If t<pt < p, line L8 sets hi←phi \leftarrow p: then lo≤t<plo \le t < p, and the items now to the right of the block are B[p…hi−1]B[p \ldots hi-1] of the old block, which are at least xx and so at least each item of B[lo…p−1]B[lo \ldots p-1], together with the old right part, which (I) bounds below by each item of the old block. If t≥qt \ge q, line L8 sets lo←qlo \leftarrow q, and the argument is the mirror one. Line L9 changes only the counter. So (I) holds again.
  3. Termination. Each pass that does not return replaces [lo,hi)[lo, hi) by [lo,p)[lo, p) or [q,hi)[q, hi), which leaves out the q−p≥1q - p \ge 1 copies of xx. So hi−lohi - lo drops by at least 11, and the loop runs at most n−1n - 1 passes.

Conclusion. The run ends in one of three ways.

  • Line L3 returns MOM-SELECT(B[lo…hi−1],t−lo+1)(B[lo \ldots hi-1], t - lo + 1). By (I), 1≤t−lo+1≤hi−lo1 \le t - lo + 1 \le hi - lo, so the rank is valid, and the call stops and returns D[t−lo]D[t - lo], which is sts_t by the consequence of (I).
  • Line L6 returns xx with p≤t<qp \le t < q. After line L5 the block is B[lo…p−1]<xB[lo \ldots p-1] < x, then q−pq - p copies of xx, then B[q…hi−1]>xB[q \ldots hi-1] > x, so the sorted block DD has D[i−lo]=xD[i - lo] = x for p≤i<qp \le i < q. The partition kept (I) true, since it only permuted the block, so by the consequence of (I), st=D[t−lo]=xs_t = D[t - lo] = x.
  • The loop stops with hi−lo=1hi - lo = 1 and line L10 returns B[lo]B[lo]. By (I), lo≤t<lo+1lo \le t < lo + 1, so t=lot = lo and D=[B[lo]]D = [B[lo]]; by the consequence of (I), st=B[lo]s_t = B[lo].

In each case the result is st=sk−1s_t = s_{k-1}. ■\blacksquare

Back to Introselect