Algorithm Search⌘K

Proofs: Uniform binary search

These are the human proofs of Uniform binary search, at full length.

Uniform binary search returns the first index of x, or NIL, with exactly floor(lg n) + 1 order comparisons full

This proof shows: correctness, termination, complexity.

Let A[0…n−1]A[0 \ldots n-1] be sorted in non-decreasing order, n≥0n \ge 0, and let xx be a key. For the argument only, extend AA with A[−1]=−∞A[-1] = -\infty and A[n]=+∞A[n] = +\infty. Let ℓ=∣{ j:0≤j<n, A[j]<x }∣\ell = |\{\, j : 0 \le j < n,\ A[j] < x \,\}| be the lower bound of xx. Since AA is sorted, A[j]<xA[j] < x for −1≤j<ℓ-1 \le j < \ell and A[j]≥xA[j] \ge x for ℓ≤j≤n\ell \le j \le n. We prove that UNIFORM-BINARY-SEARCH(A,x)(A, x) returns ℓ\ell when ℓ<n\ell < n and A[ℓ]=xA[\ell] = x, and NIL otherwise, that it reads only cells A[0],…,A[n−1]A[0], \ldots, A[n-1], and that it makes exactly ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1 order comparisons when n≥1n \ge 1.

If n=0n = 0, line L1 returns NIL, which is correct. Assume n≥1n \ge 1. Line L2 sets p=2kp = 2^k with k=⌊lg⁡n⌋k = \lfloor \lg n \rfloor, so p≤n<2pp \le n < 2p. Line L4 reads A[p−1]A[p - 1], a real cell since 1≤p≤n1 \le p \le n.

Invariant. Before each test of line L6:

  1. ss is a power of 2 with 1≤s≤p1 \le s \le p.
  2. −1≤b-1 \le b and b+s≤nb + s \le n.
  3. A[b]<x≤A[b+s]A[b] < x \le A[b + s], that is, b<ℓ≤b+sb < \ell \le b + s.

Start. Line L5 sets s=ps = p, so statement 1 holds. There are two cases for line L4.

  • If A[p−1]≥xA[p - 1] \ge x, then b=−1b = -1, and b+s=p−1≤n−1b + s = p - 1 \le n - 1. Also A[−1]=−∞<x≤A[p−1]A[-1] = -\infty < x \le A[p - 1].
  • If A[p−1]<xA[p - 1] < x, then b=n−pb = n - p. From n<2pn < 2p we get b≤p−1b \le p - 1, so A[b]≤A[p−1]<xA[b] \le A[p - 1] < x, and b≥0b \ge 0. Also b+s=nb + s = n and x≤A[n]=+∞x \le A[n] = +\infty.

Step. A pass runs only when s>1s > 1, so s≥2s \ge 2 and s′=s/2s' = s / 2 is a power of 2 with 1≤s′<s1 \le s' < s. The pass reads A[b+s′]A[b + s']. Since b≥−1b \ge -1 and s′≥1s' \ge 1, the index is at least 00; since b+s≤nb + s \le n and s′<ss' < s, it is at most n−1n - 1. So it is a real cell.

  • If A[b+s′]<xA[b + s'] < x, the new value is b′=b+s′b' = b + s'. Then A[b′]<xA[b'] < x, and b′+s′=b+sb' + s' = b + s, so b′+s′≤nb' + s' \le n and x≤A[b′+s′]x \le A[b' + s'].
  • Otherwise b′=bb' = b, A[b]<xA[b] < x, and x≤A[b+s′]x \le A[b + s'], with b+s′<b+s≤nb + s' < b + s \le n.

In both cases the three statements hold for (b′,s′)(b', s').

Termination and the count. Each pass halves ss, which starts at p=2kp = 2^k, so the loop makes exactly kk passes and stops with s=1s = 1. Each pass makes one order comparison, and line L4 makes one, so the total is k+1=⌊lg⁡n⌋+1k + 1 = \lfloor \lg n \rfloor + 1 for every key.

The result. At the end s=1s = 1, and statement 3 gives b<ℓ≤b+1b < \ell \le b + 1, so ℓ=b+1\ell = b + 1. If b+1<nb + 1 < n and A[b+1]=xA[b + 1] = x, line L9 returns ℓ\ell; since every index j<ℓj < \ell has A[j]<xA[j] < x, this is the smallest index of xx. Otherwise, either ℓ=n\ell = n, so every item is <x< x, or A[ℓ]>xA[\ell] > x, so every item from ℓ\ell on is >x> x and every item before is <x< x. In both cases xx is not in AA, and line L10 returns NIL, which is correct. ■\blacksquare

Back to Uniform binary search