zoukankan      html  css  js  c++  java
  • python 数据结构 查找数组最值

    # 找数组最小值
    arry_list = [23,65,89,778,236,665,9,995,152,66,5,668,123,3,566,89]
    def findMinAndMax_1(List):
        max = List[0]
        min = List[0]
        length = len(List)
        for n in range(1, length):
            if max < List[n]:
                max = List[n]
                # print (max)  # 65 89 778 995
            if min > List[n]:
                min = List[n]
                # print (min)  # 9 5 3
        return max, min
    
    def findMinAndMax_2(List):
        length = len(List)
        for i in range(length-1):
            if List[i] >= List[i+1]:
                temp = List[i]
                List[i] = List[i+1]
                List[i+1] = temp
            max = List[length-1]
            print(List)
        for i in range(length-1):
            if List[i] <= List[i+1]:
                temp = List[i]
                List[i] = List[i+1]
                List[i+1] = temp
            min = List[length-1]
            print(List)
        return max, min
    
    def findMinAndMax_3(List):
        for i in range(len(List)):
            for j in range(len(List)):
                if List[i] > List[j]:
                    print(List[i], List[j])
                    break
        return List[i] 
    
    
    print(findMinAndMax_1(arry_list))
    print(findMinAndMax_2(arry_list))
    print(findMinAndMax_3(arry_list))

    三种方法复杂度不同

  • 相关阅读:
    记录一次电话面试
    记录一次git合并
    HTML通用属性与常见标签
    位运算的应用
    HTML总结
    前端MVC
    常用软件
    docker常用命令
    composer install(update)时出现killed
    优化小技巧:该怎么识别百度蜘蛛呢
  • 原文地址:https://www.cnblogs.com/liuchaodada/p/13210155.html
Copyright © 2011-2022 走看看