python二分法算法

// python二分法算法
def binary_search(list,item):        //列表list中搜索item元素
    low = 0
    high = len(list) - 1
    while low < item:
        mid = int((high+low)/2)
        guess = list[mid]
        if guess = item:
            return guess
        elif guess < item:
            low = mid+1
        else:
            high = mid-1
my_list = [1,3,5,7,9,11,12]
print(binary_search(my_list,5))

猜你喜欢

转载自blog.csdn.net/weixin_42011265/article/details/84728216