Last active
April 4, 2016 13:19
-
-
Save zry656565/77790e564010aedb8a87ab6d0c0f78ba 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 binarySearch(array, start, end, target) { | |
while (true) { | |
if (start > end) return -1; | |
if (end - start < 2) { | |
if (target === array[end]) return end; | |
else if (target === array[start]) return start; | |
else return -1; | |
} | |
var mid = Math.floor((start + end) / 2); | |
if (target === array[mid]) return mid; | |
else if (target < array[mid]) end = mid; | |
else start = mid + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment