1. 程式人生 > >python二分法演算法

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))