Ad
Code
Diff
  • function binarySearch(numbers, element) {
    
      let left = 0;
      let right = numbers.length - 1;
      let pivot = Math.floor((left + right) / 2);
      while(left < pivot && pivot < right) {
        if(element < numbers[pivot]) {
          right = pivot;
        } else {
          left = pivot;
        }
        pivot = Math.floor((left + right) / 2);
      }
      if(element === numbers[pivot]) {
        return pivot;
      }
      return -1;
    }
    • function binarySearch(numbers, element) {
    • let left = 0;
    • let right = numbers.length - 1;
    • let pivot = Math.floor((left + right) / 2);
    • while(left < pivot && pivot < right) {
    • if(element < numbers[pivot]) {
    • right = pivot;
    • } else {
    • left = pivot;
    • }
    • pivot = Math.floor((left + right) / 2);
    • }
    • if(element === numbers[pivot]) {
    • return pivot;
    • }
    • return -1;
    • }