Created
February 15, 2020 16:28
-
-
Save alldayalone/a5dc27e7a3ce16ffa32c42368d917d5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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