Skip to content

Instantly share code, notes, and snippets.

@laniehei
Last active July 6, 2018 03:26
Show Gist options
  • Save laniehei/692bee78e579c16d1e71428af851dd3e to your computer and use it in GitHub Desktop.
Save laniehei/692bee78e579c16d1e71428af851dd3e to your computer and use it in GitHub Desktop.
function binarySearch(list, item) {
var min = 0;
var max = list.length - 1;
var 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;
}
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