Last active
January 29, 2019 15:45
-
-
Save Noitidart/eac8cb37ee402237319a174a0a2327df to your computer and use it in GitHub Desktop.
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 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