def binary_search(arr , target):
r = len(arr) - 1
l = 0
while l <= r:
m = (l + r) // 2
if arr[m] == target:
return m
if arr[m] > target:
r = m - 1
binary_search(arr[l:r+1], target)
if arr[m] < target:
l = m + 1
binary_search(arr[l:r+1], target)
return
list = [8, 12, 19, 22, 32, 33, 45, 97]
a = binary_search(list, 33)
print(a)
二分查找 python
最新推荐文章于 2024-04-17 21:49:47 发布