0 votes
in JavaScript by
Write a function that performs binary search on a sorted array.

1 Answer

0 votes
by

function binarySearch(arr,value,startPos,endPos){

  if(startPos > endPos) return -1;

  let middleIndex = Math.floor(startPos+endPos)/2;

  if(arr[middleIndex] === value) return middleIndex;

  elsif(arr[middleIndex > value]){

    return binarySearch(arr,value,startPos,middleIndex-1);

  }

  else{

    return binarySearch(arr,value,middleIndex+1,endPos);

  }

}   

...