Last active
May 19, 2017 04:19
-
-
Save element6/c320b856f894a55620e8d80a75b5e07a to your computer and use it in GitHub Desktop.
binary search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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