2019年8月25日 星期日

二進位搜尋法

適用於完成排序的清單

def binary_search(list,item):
    low=0
    high=len(list)-1
    while low<=high:
        mid=round((low+high)/2)
        guess=list[mid]
        if guess==item:
            return mid
        if guess>item:
            high=mid
        else:
            low=mid
    return None
my_list=[1,3,5,7,9,11]
print(binary_search(my_list,5))


====上述的程式 可能有誤
https://hugheschung.blogspot.com/2019/01/python3.html

沒有留言:

張貼留言