Skip to content

Instantly share code, notes, and snippets.

@Jagathishrex
Created November 30, 2019 17:23
Show Gist options
  • Save Jagathishrex/a46f05fdcd10c5b55a773b3c28379383 to your computer and use it in GitHub Desktop.
Save Jagathishrex/a46f05fdcd10c5b55a773b3c28379383 to your computer and use it in GitHub Desktop.
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