Created
November 5, 2021 16:38
-
-
Save ramsunvtech/f09934c7938caa7a7cac8aeea32d2e01 to your computer and use it in GitHub Desktop.
Binary Search Recursive
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 searchRecursive(inputArray, searchTerm, left, right) { | |
if (left > right) { | |
return false; | |
} | |
const midPoint = Math.floor((left + right) / 2); | |
if (inputArray[midPoint] === searchTerm) { | |
return true; | |
} else if (searchTerm < inputArray[midPoint]) { | |
return searchRecursive(inputArray, searchTerm, left, midPoint - 1); | |
} else { | |
return searchRecursive(inputArray, searchTerm, midPoint + 1, right); | |
} | |
} | |
function binarySearch(inputArray, searchTerm) { | |
return searchRecursive(inputArray, searchTerm, 0, inputArray.length - 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment