Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
Last active December 12, 2015 06:19
Show Gist options
  • Save jdjkelly/4728352 to your computer and use it in GitHub Desktop.
Save jdjkelly/4728352 to your computer and use it in GitHub Desktop.
Binary search on sorted array of ints
binarySearch = (array, key) ->
lo = 0
hi = array.length -1
while lo <= hi
mid = lo + (hi - lo) /2
if(key < array[mid]
hi = mid - 1
else if key > array[mid]
lo = mid + 1
else
mid
-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment