Binary search algorithm

  1. read n, the number of entries in the phone book
  2. read N1 ,..., Nn, T1 ,..., Tn
  3. read NAME
  4. beginning 1
  5. end n
  6. found false
  7. repeat
  8.   middle (beginning + end)/2
  9.   if NAME = Nmiddle then
  10.       write Tmiddle
  11.       found true
  12.   else
  13.       if NAME < Nmiddle then
  14.           end middle - 1
  15.       else
  16.           beginning middle + 1
  17. until either found or beginning > end
  18. if not found then
  19.   write "Sorry, but the name is not in the directory."
  20. STOP

There are major assumptions made in lines 8 and 13, what are they?

 

[Prev][TOC][Next]


CS1104 Main Page
Last Updated 01/05/2000
© L.Heath, 2000