Skip to content

Instantly share code, notes, and snippets.

@element6
Last active May 19, 2017 04:19
Show Gist options
  • Save element6/c320b856f894a55620e8d80a75b5e07a to your computer and use it in GitHub Desktop.
Save element6/c320b856f894a55620e8d80a75b5e07a to your computer and use it in GitHub Desktop.
binary search
#http://code.activestate.com/recipes/81188-binary-search/
def binary_search(seq, t):
min = 0
max = len(seq) - 1
while True:
if max < min:
return -1
m = min + (max - min) // 2
if seq[m] < t:
min = m + 1
elif seq[m] > t:
max = m - 1
else:
return m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment