编程语言
首页 > 编程语言> > 5-有序表顺序查找算法

5-有序表顺序查找算法

作者:互联网

# 算法时间复杂度O(N)
def orderedSequentialSearch(alist, item):
    pos = 0
    found = False
    stop = False
    while pos < len(alist) and not found and not stop:
        if alist[pos] == item:
            found = True
        else:
            if alist[pos] > item:
                stop = True
            else:
                pos += 1
    return found


testlist = [1, 32, 32, 23, 4, 6, 8, 32, 577, 34]
print(orderedSequentialSearch(testlist, 233))
print(orderedSequentialSearch(testlist, 577))

标签:testlist,stop,alist,pos,算法,查找,有序,orderedSequentialSearch,found
来源: https://www.cnblogs.com/lotuslaw/p/13968778.html