def binary_search(list, item):
low = 0
high = len(list)
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
return None
mylist = [1,3,4,7,9]
print(binary_search(mylist, 9))
二分法 找下标
最新推荐文章于 2023-04-22 15:51:32 发布