Skip to content

Instantly share code, notes, and snippets.

@manudevcode
Created October 1, 2020 22:05
Show Gist options
  • Save manudevcode/ed1846760fb7dfafbddc0afa241bd040 to your computer and use it in GitHub Desktop.
Save manudevcode/ed1846760fb7dfafbddc0afa241bd040 to your computer and use it in GitHub Desktop.
function binarySearch(value, list) {
let first = 0;
let last = list.length - 1;
let position = -1;
let found = false;
let middle;
while (found === false && first <= last) {
middle = Math.floor((first + last)/2);
if (list[middle] == value) {
found = true;
position = middle;
} else if (list[middle] > value) {
last = middle - 1;
} else {
first = middle + 1;
}
}
return position;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment