Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Created November 5, 2021 16:38
Show Gist options
  • Save ramsunvtech/f09934c7938caa7a7cac8aeea32d2e01 to your computer and use it in GitHub Desktop.
Save ramsunvtech/f09934c7938caa7a7cac8aeea32d2e01 to your computer and use it in GitHub Desktop.
Binary Search Recursive
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