def binary_search(list, val):
low, high = 0, len(list) -1
while low <= high:
mid = (low + high)//2
if list[mid] > val:
high = mid -1;
elif list[mid] < val:
low = mid + 1
else:
print ("mid :%d" % mid)
return mid
return -1
if __name__ == '__main__':
a = [1, 3, 53, 70, 89]
c = binary_search(a, 3)
print ("c :%d" % c)
assert(binary_search(a, 3) == 1)
binary_search() in python
最新推荐文章于 2024-02-21 16:32:08 发布