Skip to content

Instantly share code, notes, and snippets.

@billmetangmo
Created March 20, 2019 18:10
Show Gist options
  • Save billmetangmo/c888195a8f05578e19b055831b02e69b to your computer and use it in GitHub Desktop.
Save billmetangmo/c888195a8f05578e19b055831b02e69b to your computer and use it in GitHub Desktop.
Binary search for a value upper than an item
def binary_search(alist, item):
first = 0
last = len(alist) - 1
found = False
while first <= last and not found:
midpoint = (first + last) / 2
if alist[midpoint] >= item:
found = True
else:
last = midpoint - 1
first = midpoint + 1
return found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment