赞
踩
# 这个已经是最优的算法了,比较n-1次 def findMax(arr): max_pivot = arr[0] for i in range(1,len(arr)): if arr[i] > max_pivot: max_pivot = arr[i] return max_pivot def findMin(arr): min_pivot = arr[0] for i in range(1,len(arr)): if arr[i] < min_pivot: min_pivot = arr[i] return min_pivot
# 两两比较得到最大的一半数组 def partition_max(arr): pointer = 0 max_arr =[] while pointer <= len(arr)-2: max_arr += [max(arr[pointer],arr[pointer+1])] pointer +=2 # 假如是奇数,加上最后一个数,偶数的话加的为空 max_arr +=arr[pointer:] return max_arr # 找最大分治的写法,也是最优的为比较n-1次 def findmax_recursive(arr): if len(arr) == 1: return arr[0] max_arr = partition_max(arr) return findmax_recursive(max_arr)
# 找最大和最小,得到最大一半分组和最小的一半分组 def partition_max_min(arr): pointer = 0 max_arr =[] min_arr =[] while pointer <= len(arr)-2: if arr[pointer] >=arr[pointer+1]: max_arr.append(arr[pointer]) min_arr.append(arr[pointer+1]) else: max_arr.append(arr[pointer+1]) min_arr.append(arr[pointer]) pointer +=2 max_arr +=arr[pointer:] min_arr +=arr[pointer:] return max_arr,min_arr # 这个也是最优的算法了 def findMaxMin(arr): max_arr,min_arr = partition_max_min(arr) min = findMin(min_arr) max = findMax(max_arr) return max,min
#%% class node: def __init__(self,value=None,miss=None): self.value = value self.miss =miss def partition_max_with_nodes(arr): pointer = 0 max_arr =[] while pointer <= len(arr)-2: if arr[pointer].value >=arr[pointer+1].value: arr[pointer].miss.append(arr[pointer+1].value) max_arr.append(arr[pointer]) else: arr[pointer+1].miss.append(arr[pointer].value) max_arr.append(arr[pointer+1]) pointer +=2 # 假如是奇数,加上最后一个数,偶数的话加的为空 max_arr +=arr[pointer:] return max_arr def findMaxSecondmax(arr): arr_node =[] for i in range(len(arr)): arr_node.append(node(arr[i],[])) def findmax_recursive(arr): if len(arr) == 1: return arr[0] max_arr = partition_max_with_nodes(arr) return findmax_recursive(max_arr) pair = findmax_recursive(arr_node) first_max = pair.value second_max_arr = pair.miss second_max = findMax(second_max_arr) return first_max,second_max arr = [4,44,5,77,8,32,18,99,377,55,33,11,12,999,678] print(arr) ##print(findMax(arr)) ##print(partition_max(arr)) ##print(findmax_recursive(arr)) #print(partition_max_min(arr)) #print(findMaxMin(arr)) print(findMaxSecondmax(arr)) [4, 44, 5, 77, 8, 32, 18, 99, 377, 55, 33, 11, 12, 999, 678] (999, 678)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。