Created
April 19, 2010 19:40
-
-
Save teepark/371492 to your computer and use it in GitHub Desktop.
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
def bsearch(lst, item): | |
bottom, top = 0, len(lst) | |
while top - bottom >= 3: | |
mid = (top + bottom) // 2 | |
c = cmp(item, lst[mid]) | |
if c < 0: | |
top = mid | |
elif c > 0: | |
bottom = mid + 1 | |
else: | |
return True | |
if item == lst[bottom]: | |
return True | |
return top - bottom == 2 and item == lst[bottom + 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment