Skip to content

Instantly share code, notes, and snippets.

@charles2588
Created June 27, 2016 08:37
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 charles2588/83991ffeb278e25aa878cd5c3e8ee4f1 to your computer and use it in GitHub Desktop.
Save charles2588/83991ffeb278e25aa878cd5c3e8ee4f1 to your computer and use it in GitHub Desktop.
https://repl.it/C6VW/1 created by charles2588
def binarySearch(arr,searchterm):
pivotpos=int((len(arr)-1)/2)
if pivotpos==0:
return False
if arr[pivotpos]==searchterm:
return arr[pivotpos]
elif arr[pivotpos]>searchterm:
return binarySearch(arr[:pivotpos],searchterm) #search left when searchterm is less than pivot
else:
return binarySearch(arr[pivotpos:],searchterm)#search right when searchterm is greater than pivot
arr=[-1,2,3,5,16,79,100]
print(binarySearch(arr,105))
print(binarySearch(arr,-10))
print(binarySearch(arr,8))
print(binarySearch(arr,16))
print(binarySearch(arr,2))
# This is Recursive implementation of binarySearch.
Python 3.5.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
>>> False
False
False
16
2
=> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment