The optimal algorithm for selecting the maximum or minimum value
For an array of length n, the lower bound of the proven maximum or minimum comparison operation is n-1. So just let the first value be the initial maximum or the initial minimum, and update the value with all values compared to this value.
def minimum (a) : minnum=a[0 ] for i in range (1 , Len (a)): if Minnum>a[i]: minnum=a[i] return mi Nnumprint (Minimum ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
Second, the simultaneous selection of the maximum and minimum value of the fast algorithm (paired comparison)
First set a pair of initial maximum minimum value (the total number is odd, the initial maximum minimum value is the first number, if the even number is the previous two comparisons), and then the remaining 22 pairs, internal comparison size, and then small with the current minimum ratio, large and current maximum ratio. In this case two numbers need to be compared three times. If you choose to calculate the maximum minimum value of 2 (n-1) comparison, only 3int (N/2) can be used in paired comparison.
Def Minmax (a): k=Len(a)ifk%2==0:if a[0]<a[1]: minnum,maxnum=a[0],a[1]Else: minnum,maxnum=a[1],a[0]Else: minnum,maxnum=a[0],a[0] forIinchRange2-k%2K2):if a[i]>a[i+1]:a[i+1],a[i]=a[I],a[i+1]if a[i]<minnum:minnum=aIif a[i+1]>maxnum:maxnum=a[i+1]returnMinnum,maxnumprint (Minmax ([1,2,3,4,5,6,7,8,9]))
Three, the desired time is a linear time selection algorithm-Random selection
Recall that a quick sort of time, select a number (random) as the benchmark, the left and right swap will be less than the baseline to the left, greater than the base of the idea of the sub-to-go, and then get this number in the array position, so that we can determine the location of the number we want, gradually narrowing the search scope.
ImportRandom def randomizedpartion(a,p,r):K=random.randint (P, R) a[k],a[r]=a[r],a[k] Value=a[r] i=p-1 forJinchRange (P,R):ifa[j]<value:i+=1A[i],a[j]=a[j],a[i] i+=1A[i],a[r]=a[r],a[i]returnI def randomizedselect(a,p,r,i):Q=randomizedpartion (a,p,r) k=q-p+1 ifK==i:returnA[Q]Else:ifI<k:returnRandomizedselect (a,p,q-1IElse:returnRandomizedselect (a,q+1, r,i-k) lst=[2,6,3,1,5,0,7,8,4,9]k=randomizedselect (LST,0, Len (LST)-1,Ten) Print (k)
The worst time is the selection algorithm of linear time-the median division of the median number
Based on the quick-sort discard method, the average half of the number is discarded at a time, regardless of the direct hit situation.
The idea of the select algorithm is that all the numbers in the array are grouped first, the size of each group is odd, then the median of each group is computed, then the median of all the groups is computed, the median is obtained in the array position, and then the comparison is discarded. So the lower limit of each discard is not based on the fast algorithm of 0.
def midnum(a):K=len (a) groupnum=5start=0 whileStart<k:end=min (K-1, start+4) Insertsort (a,start,end) start+=5 ifk<5:returna[(K-1)//2]Else: b=a[(groupnum//2):: Groupnum]returnMidnum (b) def insertsort(a,start,end): forKinchRange (start+1, end+1): I=k whilea[i-1]>a[i] andi>start:a[i-1],a[i]=a[i],a[i-1] i-=1 returnA def partionbyvalue(a,x):I=0j=0 forKinchRange0, Len (a)):ifA[K]<X:A[K],A[I]=A[I],A[K] i+=1 forKinchRange (I,len (a)):ifA[k]==x:j=k A[i],a[j]=a[j],a[i]returnI def Select(a,i):Mid=midnum (a) k=partionbyvalue (A,mid) +1 ifI==k:returnMidElse:ifI<k:returnSelect (a[0: K-1],i)Else:returnSelect (a[k::],i-k) lst=[2,6,3,1,5,0,7,8,4,9]k=select (LST,Ten) Print (k)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Algorithms-median and sequential statistics