binarySearch<T extends Comparable<Object>> function

int binarySearch<T extends Comparable<Object>>(
  1. List<T> sortedList,
  2. T value
)

Returns the position of value in the sortedList, if it exists.

Returns -1 if the value is not in the list. Requires the list items to implement Comparable and the sortedList to already be ordered.

Implementation

int binarySearch<T extends Comparable<Object>>(List<T> sortedList, T value) {
  int min = 0;
  int max = sortedList.length;
  while (min < max) {
    final int mid = min + ((max - min) >> 1);
    final T element = sortedList[mid];
    final int comp = element.compareTo(value);
    if (comp == 0) {
      return mid;
    }
    if (comp < 0) {
      min = mid + 1;
    } else {
      max = mid;
    }
  }
  return -1;
}