repl.it
@charles2588/

BinarySearch

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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. 
?