二分查找

二分查找

头条面试题

"""
def bin_search(li, value , low, high):

    if low<=high:
        mid = (low+high) // 2

        if li[mid] == value:
            return mid
        elif li[mid] > value:
            return bin_search(li, value, low, mid-1)
        else:
            return bin_search(li, value, mid+1, high)
    else:
        return

li = [1,2,3,4,5,6,7,8,9]
index = bin_search(li, 3, 0, len(li)-1)
print(index)
"""
posted @ 2020-04-08 23:20  alen_zhan  阅读(112)  评论(0编辑  收藏  举报
返回顶部