Created
November 30, 2019 17:23
-
-
Save Jagathishrex/a46f05fdcd10c5b55a773b3c28379383 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 search(array, searchElement) { | |
let low = 0; | |
let high = array.length - 1; | |
while(low <= high) { | |
let mid = Math.floor((low+high) /2); | |
let middleElement = array[mid]; | |
if(middleElement == searchElement) { | |
return mid; | |
} else if(middleElement > searchElement) { | |
high = mid -1; | |
} else if(middleElement < searchElement) { | |
low = mid +1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment