code: public static <T> int binarySearch (
          T[] array, int offset, int length, T key, Comparator<T> comp)
      {
          int low = offset, high = offset+length-1;
          while (low <= high) {
              int mid = (low + high) >>> 1;
              T midVal = array[mid];
              int cmp = comp.compare(midVal, key);
              if (cmp < 0) {
                  low = mid + 1;
              } else if (cmp > 0) {
                  high = mid - 1;
              } else {
                  return mid; // key found
              }
          }
          return -(low + 1); // key not found.
      }
docs: Performs a binary search, attempting to locate the specified
  object. The array must be in the sort order defined by the supplied
  {@link Comparator} for this to operate correctly.
  
  @param array the array of objects to be searched.
  @param offset the index of the first element in the array to be* considered.
  @param length the number of elements including and following the
  element at <code>offset</code> to consider when searching.
  @param key the object to be located.
  @param comp the comparator to use when searching.
  
  @return the index of the object in question or
  <code>(-(<i>insertion point</i>) - 1)</code> (always a negative
  value) if the object was not found in the list.