Skip to content

Instantly share code, notes, and snippets.

@ashish0x90
Created April 19, 2010 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashish0x90/371480 to your computer and use it in GitHub Desktop.
Save ashish0x90/371480 to your computer and use it in GitHub Desktop.
def bsearch(arr,key,start=0,end=None):
if end == None: end = len(arr) - 1
if start > end: return None
if start == end and arr[start] != key: return None
mid = (start+end)/2
if arr[mid] == key:
return mid
if arr[mid] > key:
return bsearch(arr,key,start,mid-1)
if arr[mid] < key:
return bsearch(arr,key,mid+1,end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment