Skip to content

Instantly share code, notes, and snippets.

@N-Asadnajafi
Created July 6, 2019 13:48
Show Gist options
  • Save N-Asadnajafi/830d4e8f90429186b55ad16b64402299 to your computer and use it in GitHub Desktop.
Save N-Asadnajafi/830d4e8f90429186b55ad16b64402299 to your computer and use it in GitHub Desktop.
Simple Recursive Binary Search Java Script
function binarySearch(Array,startIndex, endIndex, x){
middleIndex= Math.floor((startIndex + endIndex)/2);
if(startIndex > endIndex)
return -1;
if (x == Array[middleIndex])
return middleIndex;
else if (x < Array[middleIndex])
return binarySearch(Array, startIndex, middleIndex -1, x);
else
return binarySearch(Array, middleIndex + 1, endIndex, x);
}
if (result == -1){
console.log("it is not in here!");
}
else{
console.log("The index of element:" + result);
}
//You can see it here: https://codepen.io/NargesAsadnajafi/pen/qzKqEP?editors=0012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment