# 顺序查找:无序表查找代码
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 发布
本文对比了顺序查找和有序表查找两种算法的Python实现,通过实例展示了在无序和有序列表中搜索特定元素的过程。重点介绍了如何提高查找效率,适用于初学者理解查找算法基本原理。
325

被折叠的 条评论
为什么被折叠?



