Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Created November 5, 2021 16:45
Show Gist options
  • Save ramsunvtech/2e7d7eeabbb78363d51f45aada0694de to your computer and use it in GitHub Desktop.
Save ramsunvtech/2e7d7eeabbb78363d51f45aada0694de to your computer and use it in GitHub Desktop.
Binary Search Iterative
function binarySearchIterative(inputArray, searchTerm) {
let left = 0;
let right = inputArray.length - 1;
while(left <= right) {
const midPoint = Math.floor((left + right) / 2);
if (inputArray[midPoint] === searchTerm) {
return true;
} else if (searchTerm < inputArray[midPoint]) {
right = midPoint - 1
} else {
left = midPoint + 1;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment