# 顺序查找:无序表查找代码
def sequentialSearch(alist,item):
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item:
found = True
else:
pos += 1
return found
testlist = [1,2,34,56,54,34,22,56,78]
print(sequentialSearch(testlist,33))
print(sequentialSearch(testlist,34))
# 有序表查找代码
def orderrdSequentialSearch(alists,items):
pos = 0
found = False
stop = False
while pos < len(alists) and not found and not stop:
if alists[pos] == items:
found = True
else:
if alists[pos] > items:
stop = True
else:
pos += 1
return found
testlists = [1,2,3,4,5,6,7,8,9,10]
print(orderrdSequentialSearch(testlists,4))
print(orderrdSequentialSearch(testlists,45))
数据结构与算法------------顺序查找和无序查找python实现
最新推荐文章于 2022-11-12 22:56:44 发布