欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用python实现二分查找

程序员文章站 2024-03-17 14:34:58
...
    def bisearch(elems, key):
        low = 0 
        high = len(elems) - 1
        while low <= high:
            mid = (low + high)//2
            if key == elems[mid].key:
                return elems[mid].value
            if key < elems[mid].key:
                high = mid - 1
            else:
                low = mid + 1