Homework 5 (assigned July 20, due July 26)
Exercises 7, 15, 16, 19, 21, and 24 on pages 148-149. (5 points each)
binary_search(s in nondecreasing order, n, key)
01 i := 1
02 j := n
03 while i <= j
04 m := floor[(i+j)/2]
05 if key = s[m] then return(m)
06 if key < s[m] then j := m-1
07 else i := m+1
08 end(while)
09 return(0)
(a) In the worst case, how many times is the while loop test i ≤ j on line 3 evaluated when the size of the input list is n? (3 points)
(b) Give an example to show that if the input is not in nondecreasing order, the algorithm may not find key even if it is present. (3 points)