Python實現二分查詢
阿新 • • 發佈:2020-12-18
技術標籤:Python
# 二分查詢
def func(alist, item):
low = 0
high = len(alist)-1
n = 0
while low <= high:
mid = int((low + high)/2)
n += 1
if alist[mid]==item:
return mid
if alist[mid]<item:
low = mid + 1
else:
high = (mid-1)
return None