Created
November 12, 2021 07:03
-
-
Save 9bany/67bf69386f8d799597d5b5138f70e2eb to your computer and use it in GitHub Desktop.
Binary Search - Javascript
This file contains 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 banarySearch(key, arr, min, max, callBack) { | |
if (min >= max) { | |
return undefined; | |
} | |
const midIndex = parseInt(min + (max - min) / 2, 0.0); | |
const result = callBack(arr[midIndex]); | |
if (result > key) { | |
return banarySearch(key, arr, min, midIndex, callBack); | |
} else if (result < key) { | |
return banarySearch(key, arr, midIndex + 1, max, callBack); | |
} else { | |
return midIndex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage