Skip to main content

Recursive binary search

We consider the abstraction of binary search. Given a dictionary, our goal is to find word WW in the dictionary. This abstraction can be implemented by the following pseudocode.

SEARCH(DD,WW)

  1. If DD has only one page, look for WW in DD line-by-line
    • Return the definition or report that WW is not in DD
  2. Otherwise, let WW' be the first word on the middle page of DD
    • If WW should come before WW', SEARCH(first half of DD, WW)
    • Otherwise, SEARCH(second half of DD, WW)
Theorem 1

The procedure correctly finds the definition of WW in DD (if it exists).

Proof:

We prove by induction on nn - the number of pages of DD. In the base case when DD has only one page, the procedure would look for WW line-by-line, so it is always correct. Otherwise, assume that the procedure is correct for all nn0n \leq n_0. We want to prove that it is correct when DD has n0+1n_0+1 pages. Notice that each half contains at most n0n_0 pages. There are two cases. If WW should be before WW', then we know that WW is not in the second half of DD, and thus the first subroutine on the first half will correctly (by induction hypothesis) find WW (if it exists); otherwise, if WW should be after WW' (or it is WW' itself), then we know that WW is not in the first half of DD, and thus the subroutine on the second half will correctly find WW (if it exists).