Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created December 7, 2011 21:53
Show Gist options
  • Select an option

  • Save thinkphp/1444865 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/1444865 to your computer and use it in GitHub Desktop.
Binary Search.py
'''
In computer science, a binary search algorith finds the position of a specified input key within a sorted vector
by Adrian Statescu <adrian@thinkphp.ro>
MIT Style License
'''
def binarySearch(what,arr):
li = 0
ls = len(arr)-1
while li<=ls:
middle = (li+ls)/2
if(what == arr[middle]):
return middle
elif what > arr[middle]:
li = middle + 1
else:
ls = middle - 1
return False
def binarySearchRec(what,li,ls,arr):
middle = (li+ls)/2
if what == arr[middle]:
return middle
elif what > arr[middle]:
return binarySearchRec(what,middle+1,ls,arr)
else:
return binarySearchRec(what,li,middle-1,arr)
if __name__ == "__main__":
arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
#assert 19 == binarySearch(20,arr)
print binarySearchRec(1,0,len(arr),arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment