python实现二分查找(二分查询)

二分查询算法

def binarysearch(a, target):
low = 0
high = len(a) – 1
while low > 1,java中是int mid = (low + high) >>> 1。
mid = (low + high) // 2
midval = a[mid]
if midval < target: low = mid + 1 elif midval > target:
high = mid – 1
else:
return mid
return -1

Posted in 未分类

发表评论