Skip to content

Instantly share code, notes, and snippets.

@eengineergz
Created February 27, 2021 05:20
Show Gist options
  • Save eengineergz/eb8d1e1684db15cc2c8af28e13f38751 to your computer and use it in GitHub Desktop.
Save eengineergz/eb8d1e1684db15cc2c8af28e13f38751 to your computer and use it in GitHub Desktop.
function binarySearch(array, target) {
let start = 0;
let end = array.length - 1;
while (start <= end) {
let midpoint = Math.floor((start + end) / 2);
if (target === array[midpoint]) {
return midpoint;
}
if (target > array[midpoint]) {
start = midpoint + 1;
}
if (target < array[midpoint]) {
end = midpoint - 1;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment