Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active January 29, 2019 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/eac8cb37ee402237319a174a0a2327df to your computer and use it in GitHub Desktop.
Save Noitidart/eac8cb37ee402237319a174a0a2327df to your computer and use it in GitHub Desktop.
function testIfExistsByBinarySearch(a, x) {
let il = 0;
let ir = a.length;
while (il < ir) {
const midIndex = Math.floor((ir + il) / 2);
const midValue = a[midIndex];
if (midValue < x) {
// search right half
il = midIndex + 1
} else if (midValue > x) {
// search left half
ir = midIndex;
} else {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment