Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.