UNB/ CS/ David Bremner/ teaching/ java/ Esearch.java
import ccj.*;

public class Esearch
{  public static void main(String[] args)
   {  Employee[] staff = new Employee[5];
      staff[0] = new Employee("Cracker, Carl", 48000.0);
      staff[1] = new Employee("Hacker, Harry", 35000.0);
      staff[2] = new Employee("Lam, Larry", 78000.0);
      staff[3] = new Employee("Reindeer, Rudolf", 63000.0);
      staff[4] = new Employee("Sandman, Susan", 51500.0);

      System.out.print
         ("Enter name of employee to search for: ");
      String name = Console.in.readLine();
      int i = binarySearch(staff, 0, staff.length - 1,
         name);
      if (i >= 0)
         System.out.println(staff[i].getName() + " "
            + staff[i].getSalary());
      else
         System.out.println("Not found.");
   }

   public static int binarySearch(Employee[] v, int from,
      int to, String name)
   {  if (from > to)
         return -1;
      int mid = (from + to) / 2;
      int diff = v[mid].getName().compareTo(name);
      if (diff == 0) 
         return mid;
      else if (diff < 0) 
         return binarySearch(v, mid + 1, to, name);
      else
         return binarySearch(v, from, mid - 1, name);
   }
}