Skip to content

Instantly share code, notes, and snippets.

@banyan
Created December 7, 2009 15:42
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 banyan/250877 to your computer and use it in GitHub Desktop.
Save banyan/250877 to your computer and use it in GitHub Desktop.
# coding: utf-8
def bin_search(array, search)
lowerbound = 0
upperbound = array.size - 1
while (lowerbound <= upperbound)
middle = (lowerbound + upperbound) / 2
return middle if (search == array[middle])
if (search < array[middle])
upperbound = middle - 1
else
lowerbound = middle + 1
end
end
return -1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment