def binary_search(list1,item):
low = 0
high= len(list1)-1
while low <= high:
mid = (low+high)//2
guess = list1[mid]
if guess == item:
return mid
elif guess > item:
high = mid-1
else:
low = mid+1
return None
list1 = [1, 3, 4, 6, 8, 10, 11, 13]
print(binary_search(list1, 8))
def binary_search(list1, items):
low = 0
high = len(list1)-1
while low <= high:
mid = (low+high)//2
if items == list1[mid]:
return mid
elif items < list1[mid]:
high = mid-1
return binary_search(low, high)
elif items > list1[mid]:
low = mid+1
return binary_search(low, high)
return None
list1 = [1, 2, 4, 6, 8, 10, 12, 14, 18, 19, 20] # 必须是有序的数组才能够进行二分查找。
print(binary_search(list1, 10))