Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Last active October 10, 2017 13:50
Show Gist options
  • Save jmlavoier/166e1c2304496c06ece7264229a79f1d to your computer and use it in GitHub Desktop.
Save jmlavoier/166e1c2304496c06ece7264229a79f1d to your computer and use it in GitHub Desktop.
let binarySearch = function (array, valueSearch) {
let start = 0;
let end = array.length - 1;
return function search(start, end) {
let mid = Math.round((start + end) / 2);
let length = end - start;
if (length < 0) return -1;
if (array[mid] === valueSearch) {
return mid;
} else if (array[mid] > valueSearch) {
return search(start, mid - 1);
} else if (array[mid] < valueSearch) {
return search(mid + 1, end);
}
}(start, end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment