Bare Bones Binary Search Example¶
In [1]:
def binSearch(Alist, v):
low = 0
high = len(Alist)-1
while low<=high:
mid = (low+high)//2 # floor division
if Alist[mid] < v:
low=mid+1
elif Alist[mid] > v:
high = mid-1
else:
return True
return False
In [2]:
sortedList = [0,2,4,8,16,18,19]
for x in range(21):
print(x,binSearch(sortedList,x))