10.9 Review

  1.  
    0 12 Fox
    7 12 Owl
    7 8 Gnu
    8 8 Hen
  2.  
    public static int seqSearchSorted (String[] list, String item)
    {
       int location = -1;
       for (int i = 0; i < list.length && item.compareTo(list[i]) >= 0; i++)
       {
          if (item.equals(list[i]))
          {
             location = i;
             i = list.length;
          }
       }
       return location;
    } 
  3.  
    public static int binSearch (double[] list, double item)
    {
       int bottom = 0;
       int top = list.length - 1;
       int middle;
       boolean found = false;
       int location = -1;
       while (bottom <= top && !found)
       {
          middle = (bottom + top)/2;
          if (list[middle] == item)
          {
             found = true;
             location = middle;
          }
          else if (list[middle] < item)
             bottom = middle +1;
          else
             top = middle -1;
       }
       for (int i = location; i >= 0 && list[i] == item; i--)
          location = i;
       return location;
    } 
    1.  
      5 1 4 8 3 6 9
      5 1 4 6 3 8 9
    2.  
      1 5 4 8 9 6 3
      1 4 5 8 9 6 3
    3.  
      1 4 5 8 6 3 9
      1 4 5 6 3 8 9
  4.  
    53 13 14 50 12 70 29 26 61 54 86 75 70 65
    1. Exchange each element of the list with a random item of the list.
    2.  
      public static void shuffle (int[] list)
      {
         int temp, random;
         for (int i = 0; i < list.length; i++)
         {
            random = (int)(Math.random() * list.length);
            temp = list[i];
            list[i] = list[random];
            list[random] = temp;
         }
      }