二分查找准确来讲是指数式的查找,每次都排除一半
def binary_search(list,item):
"""传入两个参数,一个是列表list,一个是要查找的对象"""
low=0
high=len(list)-1
while low<=high: #只要范围没有缩小到只包含一个元素,就检查中间的元素
mid=int((low+high)/2)
guess=list[mid]
if guess==item:
return mid
if guess>item:
high=mid-1
else:
low=mid+1
my_list=[1,3,5,7,9]
print(binary_search(my_list,3))
print(binary_search(my_list,0))