您好,下列是搜索最小值的参考样例:
def indexOfMin(lyst):# 搜索最小值
minIndex = 0
currentIndex = 1
while currentIndex < len(lyst):
if lyst[currentIndex] < lyst[minIndex]:
minIndex = currentIndex
currentIndex += 1
return minIndex
def sequentialSearch(target,lyst):#顺序搜索
position = 0
while position < len(lyst):
if target == lyst[position]:
return position
position += 1
return -1
def binarySearch(target,sortedLyst):#二叉搜索,序列必须是有序的
left = 0
right = len(sortedLyst)-1
while left <= right:
midpoint = (left+right)//2
if target == sortedLyst[midpoint]:
return midpoint
elif target < sortedLyst[midpoint]:
right = midpoint - 1
else:
left = midpoint + 1
return -1
def main():
lyst = [1,2,6,43,2,76,-4,43,326,46]
index = indexOfMin(lyst)
print(index)
if name == "__main__":
main()
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。