binarySearch method

dynamic binarySearch(
  1. dynamic x
)

Implementation

binarySearch(x) {
  var dist = this.distribution!;
  var start = 0;
  var end = dist.length - 1;

  var index = -1;

  while (start <= end) {
    var mid = Math.ceil((start + end) / 2);

    if (mid == 0 || dist[mid - 1] <= x && dist[mid] > x) {
      index = mid;

      break;
    } else if (x < dist[mid]) {
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }

  return index;
}