Skip to content

Instantly share code, notes, and snippets.

@clemesha-ooi
Created April 19, 2010 21:35
Show Gist options
  • Save clemesha-ooi/371673 to your computer and use it in GitHub Desktop.
Save clemesha-ooi/371673 to your computer and use it in GitHub Desktop.
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