开发者社区> 问答> 正文

python怎么搜东西

python怎么搜东西

展开
收起
云计算小粉 2018-05-10 20:10:57 1649 0
1 条回答
写回答
取消 提交回答
  • 阿里云大学导师

    您好,下列是搜索最小值的参考样例:
    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()
    2019-07-17 22:23:56
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Python 脚本速查手册 立即下载
Python系列直播第一讲——Python中的一切皆对象 立即下载
给运维工程师的Python实战课 立即下载