1. 程式人生 > 其它 >Python實現二分查詢

Python實現二分查詢

技術標籤: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