def b_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return True
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return False
def b_search_recur(arr, target):
if len(arr) == 1:
if target == arr[0]:
return True
else:
return False
left = 0
right = len(arr) - 1
mid = left + (right - left) // 2
if arr[mid] == target:
return True
elif arr[mid] < target:
return b_search_recur(arr[mid + 1:], target)
else:
return b_search_recur(arr[:mid], target)
二分查找(递归/非递归)
最新推荐文章于 2024-08-29 15:58:04 发布