10.2

  1. In each part the search would examine
    (a) 55 and 72
    (b) 55, 34, 49, and 41
    (c) 55, 72, 60, and 67
  2.  
    public static int binSearch (double[] list, double item)
    {
       int bottom = 0;
       int top = list.length - 1;
       int middle = 0;
       boolean found = false;
       int location = -1;
       while (bottom <= top && !found)
       {
          middle = (bottom + top) / 2;
          if (list [middle] == item)
          {
             // Success
             found = true;
             location = middle;
          }
          else if (list [middle] > item)
             // Item cannot be in bottom half
             bottom = middle + 1;
          else
             // Item cannot be in top half
             top = middle - 1;
       }
    
    
       return location;
    } 
  3.  
    public static int binSearch (double[] list, double item)
    {
       int bottom = 0;
       int top = list.length - 1;
       int middle = 0;
       boolean found = false;
       int location = -1;
       while (bottom <= top && !found)
       {
          middle = (bottom + top) / 2;
          if (list [middle] == item)
          {
             // Success
             found = true;
             location = middle;
          }
          else if (list [middle] < item)
             // Item cannot be in bottom half
             bottom = middle + 1;
          else
             // Item cannot be in top half
             top = middle - 1;
       }
    
    
       if (location == -1)
          if (list [middle] < item)
             if (middle < list.length - 1 && list [middle + 1] - item < Math.abs(list [middle] - item))
                location = middle + 1;
             else
                location = middle;
          else
             if (middle > 0 && Math.abs(list [middle - 1] - item) < list [middle] - item)
                location = middle - 1;
             else
                location = middle;
       return location;
    } 
  4. 3
  5. (a) 2 (b) 4 (c) 5 (d) 6 (e) 7 (f) 9 (g) 10 (h) 14