Skip to content

Instantly share code, notes, and snippets.

@alldayalone
Created February 15, 2020 16:28
Show Gist options
  • Save alldayalone/a5dc27e7a3ce16ffa32c42368d917d5f to your computer and use it in GitHub Desktop.
Save alldayalone/a5dc27e7a3ce16ffa32c42368d917d5f to your computer and use it in GitHub Desktop.
function binarySearch(arr, x, index = 0) {
if (arr.length == 0) {
return -1;
}
let halfN = half(arr.length);
if (arr[halfN] == x) {
return index + halfN;
}
if (arr[halfN] < x) {
return binarySearch(arr.slice(halfN), x, index + halfN);
}
return binarySearch(arr.slice(0, halfN), x, index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment