二分查找

def bin_search(array, value):
    """二分查找"""
    left = 0
    right = len(array) - 1
    while left <= right:
        middle = (left + right) // 2
        if array[middle] > value:
            right -= 1
        elif array[middle] < value:
            left += 1
        else:
            return middle
    return None

array = [1, 2, 3, 4, 5, 6]
result = bin_search(array, 6)
print(result)

 

posted @ 2019-04-08 17:13  就俗人一个  阅读(281)  评论(0编辑  收藏  举报