Created
July 6, 2019 13:48
-
-
Save N-Asadnajafi/830d4e8f90429186b55ad16b64402299 to your computer and use it in GitHub Desktop.
Simple Recursive Binary Search Java Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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