Algorithm Design: find one of the first n largest numbers from a large array

Source: Internet
Author: User

Here we will talk about a method similar to quick sorting. Pay attention to the question requirements. Full sorting is not required. You must solve the problem as soon as possible! When I interviewed Ni, the other party asked me this question. The original statement is to find the largest of the first 0.1 billion data records.

First, let's look at the source code:

Void main (int A [], int start, int end, int N) // locate the Top N largest values from array. If it is a [100], start = 0, end = 99. Pay attention to this question.

{
Int mid = (start + end)/2;

Int I = start, j = end;

While (I <j)

{
While (I <J & A [I] <= A [Mid])

I ++;

While (I <J & A [J]> = A [Mid])

J --;

Swap (A [I], a [J]);

}
/* Pay attention that after this while, I must be equal to J, and the end from I to end is a large end */

If (end-I + 1 = N)

Return;

If (end-I + 1> N)

Findmaxn (A, I, end, N );

Else

Findmaxn (A, start, I, N-(end-I + 1 ));

}
Let's talk about the concept in detail. If you understand the fast sorting, this will not be unfamiliar. First, use a [Mid] as the reference value, and then let I and j traverse from both ends. If the data at the end of the index is smaller than the reference value a [Mid], traverse it forward, if the value on the left is greater than a [Mid], the while loop will jump out. Remember that the value of a [I] is greater than a [Mid].

Then, similar ideas are traversed from the end of J. When the data a [J] on the right is smaller than the reference value a [Mid], the small while loop will jump out. Then run the swap () function to exchange the two values. After the outermost while loop is generated, I must be equal to J. Note that I and j are not necessarily equal to the mid in the current domain. In addition, the size from I to end is large. Then, we can check the number of data records at the Large End and traverse the data.

If the number is equal to N, the function jumps out. If the value is greater than N, it is to be searched from I to end. If the value is less than N, for example, if you want to find the first 50 largest values, the end-I + 1 is equal to 20, that is, there are 20 large numbers from I to end, which need to be from start (for the first time, it can be considered as 0) in the range I, find the 50-20 = 30 largest ones.

As for swap functions, the following is implemented by reference:

Void swap (Int & A, Int & B)

{

Int temp =;

A = B;

B = temp ;}

Finally, if the execution of this function is complete, how can I access the maximum N number? Very simple, assuming the array length is N, from a [n-1], a [N-2]... The number of N is the maximum number of the first n data! The maximum time complexity of this algorithm is O (n square). The best case is O (n), and the flat result is O (n ).

When I leave it blank, I will introduce the second idea. The above code is impromptu, and the source code will be uploaded later.
Http://download.csdn.net/detail/yanzi1225627/4684046

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.