算法-查找

# 二分查找
def bin_search(li, val):
    # 有序列表

    low = 0
    high = len(li)-1
    while low <= high:
        mid = (low + high) // 2
        if li[mid] == val:
            return mid
        elif li[mid] > val:
            high = mid - 1
        else:  # li[mid]<val
            low = mid + 1
    return -1

posted @ 2020-04-21 10:38  烧刘病  阅读(83)  评论(0编辑  收藏  举报
回到页首