Created
June 27, 2016 08:37
-
-
Save charles2588/83991ffeb278e25aa878cd5c3e8ee4f1 to your computer and use it in GitHub Desktop.
https://repl.it/C6VW/1 created by charles2588
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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