http://m.blog.csdn.net/blog/zhangzhengyi03539
http://m.blog.csdn.net/blog/zhangzhengyi03539/46795831
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 Span class= "Hljs-title" >minimum (a): Minnum=a[0] for i in range (1,len (a)): if Minnum>a[i]: minnum=a[i] return minNumprint (Minimum ([ Span class= "Hljs-number" >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=LenAIf k%2==0:Ifa[0]<a[1]: minnum,maxnum=a[0],a[1]else:minnum,maxnum=a[1],a[0]else:minnum,maxnum=a[0],a[0]For IIn range (2-k%2,k,2):IfA[i]>a[i+1]: a[i+1], a[i]=a[i],a[i+1] if a[i]<minnum:minnum=a[i] if a[i+1] >maxnum:maxnum=a[i+1] Return Minnum,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.
Based on the quick-sort discard method, the average half of the number is discarded at a time, regardless of the direct hit situation.
Import RandomDefRandomizedpartion(a,p,r): K=random.randint (P, R) a[k],a[r]=a[r],a[k] Value=a[r] i=p-1For JIn range (P,R):If a[j]<value:i+=1 A[i],a[j]=a[j],a[i] i+=1 A[i],a[r]=a[r],a[i]return IDefRandomizedselect(a,p,r,i): Q=randomizedpartion (a,p,r) k=q-p+1 if k==i: return A[q] else: if i<k: return randomizedSelect (A,p, Q-1,i) else: return Randomizedselect (A,q+1,r,i-k) lst=[2, 6,3,1, 5,0,7,8, 4,9]k=randomizedselect (Lst,0,len (LST)-1,10) print (k)
The worst time is the selection algorithm of linear time-the median division of the median number
The idea of the select algorithm is to group all the numbers in the array first, then calculate the median of each group, then calculate the median of the median of all the groups, then modify the fast sorting strategy to divide by value, and return the median median in the array position. Then you can judge the abandonment.
The median-based number of one-time discards has a lower N/4 (this is a lower limit of my own calculation, the lower bound may be higher)--for example, the number of people to be found is less than the median median, the upper part of the group with the median greater than the median is sure that this round needs to be discarded, and that one of the downline is n/ 4, so that each time a maximum of the last data 3N/4 data, and each time the length of n is calculated as Theta (n), according to the nature of geometric series, its order is the same order as the first. So the worst case calculation time is theta (n).
DefMidnum(a): K=len (a) groupnum=5 start=0While Start<k:end=min (K-1,start+4) Insertsort (a,start,end) start+=5If k<5:Return a[(K-1)//2]else:b=a[(groupnum//2):: Groupnum]return Midnum (b)DefInsertsort(a,start,end):For KIn range (start+1,end+1): I=kWhile a[i-1]>a[i]and i>start:a[i-1],a[i]=a[i],a[i-1] i-=1Return aDefPartionbyvalue(a,x): i=0 j=0For KIn range (0,len (a)):If A[K]<X:A[K],A[I]=A[I],A[K] i+=1For KIn range (I,len (a)):If A[k]==x:j=k A[i],a[j]=a[j],a[i]return Idef select (a,i): Mid=midNum (a) k= Partionbyvalue (a,mid) +1 if i==k: Return mid else: if i<k: return Select (A[0:k-1],i) else: return Select (a[k::],i-k) lst=[2,6,< Span class= "Hljs-number" >3,1,5,0, 7,8,4, 9]k=select (Lst,10) print (k)
Additional notes:
Geometric Series summation problem
Geometric series General formula: A (m) =a (1) *q^ (m-1)
Geometric Series summation formula: S (M) =a (1) * (1-q^m)/(1-Q)
If a (1) =c*n
A (m+1) =q*a (M) (q<1)
Available S (m) =c*n* (1-q^m)/(1-Q) =theta (n)
In this blog, the problem three corresponds to q=0.5, the problem four corresponds to q<0.75, so are linear time algorithm
Annotations: When the order is only very large data, the lower the calculation time, the shorter the data volume, the linear time of the constant scale factor effect.
Introduction to the algorithm-median and sequential statistics