Last active
August 4, 2018 17:21
-
-
Save arton/d770719a5adaa5a787082d903bcfd71b to your computer and use it in GitHub Desktop.
binary search for Coffee
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
binsearch = (a, low, high, target) -> | |
if low == high || low + 1 == high | |
if a[low] < target then return low + 1 else return low | |
mid = Math.floor((low + high) / 2) | |
if a[mid] == target | |
return mid | |
else if target > a[mid] | |
binsearch(a, mid + 1, high, target) | |
else | |
binsearch(a, low, mid - 1, target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment