#每次排除一半的数据,效率高;局限性:必须是有序序列
应用:
lst = [11,22,33,44,55,66,77,88,99,123,234,345,456,567,678,789,1111]
def binary_search(left, right, n):
middle = (left + right)//2
if left > right:
return -1
if n > lst[middle]:
left = middle + 1
elif n < lst[middle]:
right = middle - 1
else:
return middle
return binary_search(left, right, n)
print(binary_search(0, len(lst)-1, 65) )