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))

三种方法复杂度不同

猜你喜欢

转载自www.cnblogs.com/liuchaodada/p/13210155.html