binarySearch static method

int binarySearch(
  1. List<int> arr,
  2. int value, [
  3. int start = 0,
  4. int? end,
])

Implementation

static int binarySearch(List<int> arr, int value, [int start = 0, int? end]) {
  late int mid;
  end ??= arr.length - 1;
  while (start <= end!) {
    mid = start + ((end - start) / 2).round();
    if (arr[mid] == value) return mid;
    if (arr[mid] < value) {
      start = mid + 1;
    } else {
      end = mid - 1;
    }
  }
  return -1;
}