def two_lookup(list, element):
low = 0
high = len(list) - 1
while low <= high:
mid = (low + high) // 2
if element == list[mid]:
print('list[{}]={}'.format(mid,element))
return
elif element > list[mid]:
low = mid + 1
else:
high = mid - 1
return
list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
element = 7
two_lookup(list, element)
# list[6]=7
优点 : 查找速度快, 每次都能减少一半的范围
缺点 : 只适用于有序列表
时间复杂度 : O(logn)