Colin is daily life record

导航

折半查找

def bin_search(items, key):
    '''折半查找'''
    start, end = 0, len(items) - 1
    while start <= end:
        mid = (start + end) // 2
        if key > items[mid]:
            start = mid + 1
        elif key < items[mid]:
            end = mid - 1
        else:
            return mid

    return -1

print(bin_search([3.2,2,10,3,1,45,90,0], 3))

posted on 2024-07-14 13:32  酷酷的瑞瑞  阅读(0)  评论(0编辑  收藏  举报