lib/utils.js
function binarySearch(arr, key, compareFunc) {
  let low = 0;
  let high = arr.length-1;

  while (low <= high) {
    const mid = (low + high) >>> 1;
    const midVal = arr[mid];
    const cmp = compareFunc(midVal, key);
    if (cmp < 0) {
      low = mid + 1;
    }
    else if (cmp > 0) {
      high = mid - 1;
    }
    else
    {
      //The key was found in the Array
      return mid;
    }
  }
  // key not found
  return ~low;
}
function binarySearch(arr, key, compareFunc) {
  let low = 0;
  let high = arr.length-1;

  while (low <= high) {
    const mid = (low + high) >>> 1;
    const midVal = arr[mid];
    const cmp = compareFunc(midVal, key);
    if (cmp < 0) {
      low = mid + 1;
    }
    else if (cmp > 0) {
      high = mid - 1;
    }
    else
    {
      //The key was found in the Array
      return mid;
    }
  }
  // key not found
  return ~low;
}
function binarySearch ( arr , key , compareFunc ) { let low = 0 ; let high = arr . length - 1 ; while ( low <= high ) { const mid = ( low + high ) >>> 1 ; const midVal = arr [ mid ] ; const cmp = compareFunc ( midVal , key ) ; if ( cmp < 0 ) { low = mid + 1 ; } else if ( cmp > 0 ) { high = mid - 1 ; } else { //The key was found in the Array return mid ; } } // key not found return ~ low ; }
Searches the specified Array for the provided key using the binary
search algorithm.  The Array must be sorted.
@param {Array} arr
@param key
@param {function} compareFunc
@returns {number} The position of the key in the Array, if it is found.
If it is not found, it returns a negative number which is the bitwise complement of the index of the first element that is larger than key.
Searches the specified Array for the provided key using the binary search algorithm . The Array must be sorted .