二分查找python代码
时间: 2023-10-27 22:49:14 浏览: 75
def binary_search(arr, target):
left, right = 0, len(arr)-1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1 # 如果没有找到目标元素,返回-1
阅读全文