public static final int binarySearch(Object[] a,Object key,Comparator cp,int length){
  int x1=0;
  int x2=length;
  int i=x2 / 2, c;
  while (x1 < x2) {
    c=cp.compare(a[i],key);
    if (c == 0) {
      return i;
    }
 else     if (c < 0) {
      x1=i + 1;
    }
 else {
      x2=i;
    }
    i=x1 + (x2 - x1) / 2;
  }
  return -1 * (i + 1);
}

public static final int binarySearch ( Object [ ] a , Object key , Comparator cp , int length ) { int x1 = 0 ; int x2 = length ; int i = x2 / 2 , c ; while ( x1 < x2 ) { c = cp . compare ( a [ i ] , key ) ; if ( c == 0 ) { return i ; } else if ( c < 0 ) { x1 = i + 1 ; } else { x2 = i ; } i = x1 + ( x2 - x1 ) / 2 ; } return - 1 * ( i + 1 ) ; }
perform a binary search over a sorted range of an array for the given key . the range is assumed to start at index 0 .
