Created
April 19, 2010 21:35
-
-
Save clemesha-ooi/371673 to your computer and use it in GitHub Desktop.
This file contains 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 binary_search(e, l): | |
"""Binary search for e in l. Assume l is sorted. | |
Return 'True' if e exists, 'False' if not. | |
""" | |
llen = len(l) | |
if not llen: | |
return False | |
mid = llen/2 | |
if e == l[mid]: | |
return True | |
else: | |
if e == l[mid-1]: | |
return True | |
if e < l[mid-1]: | |
return binary_search(e, l[:mid-1]) | |
else: | |
return binary_search(e, l[mid+1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment