in JavaScript by
Q:
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);

  }

}   

Click here to read more about JavaScript
Click here to read more about Insurance

Related questions

0 votes
asked Jan 29, 2020 in Other by rahuljain1
0 votes
asked Oct 23, 2020 in JAVA by rahuljain1
...