Skip to content

Instantly share code, notes, and snippets.

@achepukov
Created April 6, 2021 00:42
Show Gist options
  • Save achepukov/7cf291ae32027f5c62b383fff86d9d0b to your computer and use it in GitHub Desktop.
Save achepukov/7cf291ae32027f5c62b383fff86d9d0b to your computer and use it in GitHub Desktop.
Binary search in javascript
const binSearch = (list, val) => {
let min = 0, max = list.length - 1, index, guess;
while(min <= max) {
index = Math.ceil((min + max) / 2, 10);
guess = list[index];
if (guess === val) {
return index;
} else if (guess > val) {
max = index - 1;
} else {
min = index + 1;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment