Finding the smallest nth element in a sequence (partition function implementation)

Source: Internet
Author: User

Partition is a segmentation algorithm used to divide a sequence a[n] into three parts: A[n] greater than the portion of an element x, equal to the portion of X and less than X.

The partition program is as follows:

LongPartition (LongA[],LongP1,Longp2) {//the A[P1]~A[P2] is split, returning the sequence number of the split point, P1, p2 respectively the first of the tuple//One and the last elementLongI, J;intX;i=p1;j=p2;x=A[i]; while(i<j) { while(A[j] >= x && i,j) j--;if(I&LT;J) {A[i] = a[j]; i++;} while(A[i] <= x && i<j) i++;if(I&LT;J) {A [j] = a[i]; j--;}} A[i]=x;returni;}

The program that uses the partition function to find the nth element is as follows:

LongOrderstatistics (LongA[],LongP1,LongP2,Longk) {//in A[P1]~A[P2], find the minimum value and return the valueLongp, num;if(k<1|| k>p2-p1+1)return-1; if(P1 >= p2)returnA[P1];//if A[P1]~A[P2] has only one element, the element is returnedp =Partition (A, p1, p2); Num= PP1;if(k = = num +1)returnA[P];//the K-element is the dividing pointif(k <= num)returnOrderstatistics (A, P1, p1, k);//k small element in the front sectionreturnOrderstatistics (A, p+1, p2, k-num-1);//k small element in the back part}

The python implementation of this method is given in Python cookbook, as follows:

ImportRandomdefSelect (data, N):#Create a new list, process indexes that are less than 0, and check the validity of the indexesdata =list (data)ifn<0:n+=len (data)if  not0 <= N <len (data):RaiseValueError,"can ' t get rank%d out of%d"%(n, Len (data))#main loop, which looks similar to sort but does not require recursion whileTrue:pivot=random.choice (data) Pcount=0 under, over=[], [] uappend, Oappend=Under.append, Over.append forEleminchData:ifElem <pivot:uappend (elem)elifElem >pivot:oappend (elem)Else: Pcount+ = 1Numunder=Len (under)ifN <Numunder:data=underelifN < Numunder +Pcount:returnPivotElse: Data=Over N-= Numunder +pcount

The author mentions that it is also possible to find a K-element by using the following simple method:

def selsor (data, N):     = list (data)    Data.sort ()    return Data[n]

Both of these methods can be implemented, but the "list-based sort method is much simpler to implement, and the implementation of select does require a little more effort, but if n is large enough and the overhead of the comparison operation cannot be ignored, select shows its value." ”

Finding the smallest nth element in a sequence (partition function implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.