Skip to content

Instantly share code, notes, and snippets.

@arton
Last active August 4, 2018 17:21
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 arton/d770719a5adaa5a787082d903bcfd71b to your computer and use it in GitHub Desktop.
Save arton/d770719a5adaa5a787082d903bcfd71b to your computer and use it in GitHub Desktop.
binary search for Coffee
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