1  /**
  2     A class for executing binary searches in an array.
  3  */
  4  public class BinarySearcher
  5  {  
  6     /**
  7        Finds a value in a range of a sorted array, using the binary
  8        search algorithm.
  9        @param a the array in which to search
 10        @param low the low index of the range
 11        @param high the high index of the range
 12        @param value the value to find
 13        @return the index at which the value occurs, or -1
 14        if it does not occur in the array
 15     */
 16     public static int search(int[] a, int low, int high, int value)
 17     {  
 18        if (low <= high)
 19        {
 20           int mid = (low + high) / 2;
 21  
 22           if (a[mid] == value) 
 23           {
 24              return mid;
 25           }
 26           else if (a[mid] < value )
 27           {
 28              return search(a, mid + 1, high, value);
 29           }
 30           else
 31           {
 32              return search(a, low, mid - 1, value);
 33           }         
 34        }
 35        else 
 36        {
 37           return -1;
 38        }
 39     }
 40  }
 41