Skip to content

Instantly share code, notes, and snippets.

@MarwanShehata
Created January 7, 2024 04:06
Show Gist options
  • Save MarwanShehata/e83d68ac36364466c6a201f756dd1185 to your computer and use it in GitHub Desktop.
Save MarwanShehata/e83d68ac36364466c6a201f756dd1185 to your computer and use it in GitHub Desktop.
function binarySearch(list, item) {
let min = 0;
let max = list.length - 1;
let guess;
while (min <= max) {
guess = Math.floor((min + max) / 2);
if (list[guess] === item) {
return guess;
} else {
if (list[guess] < item) {
min = guess + 1;
} else {
max = guess - 1;
}
}
}
return -1;
}
console.log(binarySearch([2, 6, 7, 90, 103], 90));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment