The number of the nth large in the output array

Source: Internet
Author: User

It seems that some big companies have had a question: Find out the number of the nth in the array, of course a little change, but the essential part is this.

Requirements are not sortable, time complexity cannot exceed O (n^2)

A lot of ideas, I will only be quick-line derivative of the kind. If you're not familiar with fast sorting, it's recommended to review the quick sort I discussed earlier.

  

OK, now suppose you're already familiar with the quick sort.

Each round of the fast Platoon, we have to find a fulcrum, and then from the side of the array alternating starting with the fulcrum comparison, the right is smaller than the fulcrum of the number to the left, the left side of the number of points greater than the shift to the right, moving to the end, there is only one position, and then fill in the fulcrum. You find that the number on the right side of the fulcrum is greater than the fulcrum. Assuming that the Fulcrum index equals I, then the pivot point is the number of endindex-i+1. (Note: Endindex is the subscript for the last element of the array)

Remember our goal, our goal is to find the nth number, if endindex-i + 1 = n, it means we found it. But 2 numbers compare 3 results. Let's discuss it in a different way:

Remember th = endindex-i + 1,find (A, StartIndex, EndIndex, N)

(1) th = N, return pivot

(2) th > N, indicating that the number of the nth is on the right side of the fulcrum, so continue to find on the right: find (A, i + 1, endIndex, N)

(3) th < N, indicating that the number of the nth large on the left side of the fulcrum, the number on the right is larger than the number to look for, but also larger than the fulcrum, so only need to find a large number on the left (n-th), find (A, StartIndex, I-1, n-th)

  

Code:

#include <stdio.h>#include<stdlib.h>intChoose_nth (intA[],intStartIndex,intEndIndex,intn);intMainintargcChar*argv) {    intA[] = { Max,111, +, About, -,Ten,189}; intN,i; intan =sizeof(a)/sizeof(int); printf ("array: \ n");  for(i =0; I < an; ++i) printf ("%d", A[i]); printf ("\ n"); printf ("to find the number of the first few:"); scanf ("%d",&N); intAns = choose_nth (A,0, An-1, N); printf ("the number of major%d is:%d\n", n, ans); return 0;}intChoose_nth (intA[],intStartIndex,intEndIndex,intN) {    intMidone =A[startindex]; inti = startIndex, j =EndIndex; if(i = = j)//one of the recursive exits        returnA[i]; if(I <j) { while(I <j) { for(; i < J; j--)            if(A[j] <Midone) {A[i++] =A[j];  Break; }             for(; i < J; i++)            if(A[i] >Midone) {A[j--] =A[i];  Break; }} A[i]= Midone;//Fulcrum return position        intth = Endindex-i +1;//calculate the number of the first major        if(th = = N)//just found        {            returnA[i]; }        Else        {            if(Th > N)//to the right of the Fulcrum .                returnChoose_nth (A, i +1, EndIndex, N); Else//on the left side of the pivot point (n-th) large, because the right th number is larger than the Fulcrum                returnChoose_nth (A, startIndex, I-1Nth); }    }    }

Output Result:

array:  Max 111  +  About  - Ten 189 to find the number of the first few: 4 the 4th largest number is:  Max

The number of the nth large in the output array

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.