def binary_search(li, val):
left = 0
right = len(li) - 1
while left <= right:
mid = (left + right) // 2
if li[mid] == val:
return mid
elif li[mid] > val:
right = mid - 1
else:
left = mid + 1
else:
return None
li = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(binary_search(li, 3))
二分查找——python
于 2023-10-11 17:07:44 首次发布