Find the maximum number of K in N Integers

Source: Internet
Author: User

Question 1: Find the number k in N Integers

It is similar to the following class fast sorting algorithm, which can be implemented recursively. You need to verify it. Average complexity O (n ).

Question 2: Find the maximum number of K in N Integers

Two better solutions: O (nlogk)

1. Fast sorting and Recursion

Solution 2]

Recall the quick sorting. Each step in the quick sorting is to divide the data to be sorted into two groups, one of which
Any number of data is larger than any of the other groups, and then perform similar operations on the two groups respectively.
And then proceed ......

In this question, assuming n numbers are stored in array S, we randomly find
Element x divides the array into two parts: SA and Sb. The element in SA is greater than or equal to X, and the element in Sb is less than X.

There are two possibilities:

1. The number of elements in SA is smaller than K, the number in SA and the maximum K-in SB | sa | elements (| sa | meaning SA

Is the maximum K number in array S.

2. If the number of elements in SA is greater than or equal to K, the maximum K Elements in SA must be returned.

In this way, the problem is continuously decomposed into smaller problems, and the average time complexity O (N *
Log2k ). The pseudocode is as follows:

Kbig (S, k ):
If (k <= 0 ):
Return [] // return an empty array

If (length S <= k ):
Return s
(SA, Sb) = partition (s)
Return kbig (SA, k). append (kbig (SB, K-length SA)

Partition (s ):

Sa = [] // The Initialization is an empty array.

SB = []
// Select a random number as the grouping standard to avoid algorithm degradation in special data.
// You can also shuffles the entire data for preprocessing.
// Swap (s [1], s [random () % length S])
P = s [1]
For I in [2: length S]:
S [I]> P? SA. append (s [I]): SB. append (s [I])
// Adding P to a small group can avoid group failure and make the group more even and improve efficiency.
Length SA <length sb? SA. append (P): SB. append (P)
Return (SA, Sb)

2 heap sorting

Let's set N> K. The maximum K number in the first K number is a degradation. All K numbers
Is the maximum K number. What if we consider the k + 1 Number X? If X is the largest K number
If the number y is small, the maximum number of K remains unchanged. If X is larger than Y, then the maximum K
Y should be removed from the number, but X should be included. If you use an array to store the maximum K number, add one
If the number is X, scan the array again to obtain the minimum number Y in the array. Replace y with X or retain the original number.
Group unchanged. This method takes O (N * K ).

Furthermore, the minimum heap with a capacity of K can be used to store the maximum K number. Minimum heap metadata
Prime is the smallest of the maximum K numbers. Each time a new number X is considered, if X is better than the element at the top of the heap
Y is smaller, so you do not need to change the original heap because this element is smaller than the maximum K number. If X is better than heap
If the top element is large, use X to replace y on the top of the heap. After x replaces the heap top element y, X may be broken.
The structure of the minimum heap (each node is larger than its parent node), and the heap needs to be updated to maintain the heap.
Quality. The time complexity of the update process is O (log2k ).

Figure 2-1 is a heap, represented by an array H. For each element H [I], its parent node is H [I/2],
The son nodes are H [2 * I + 1] and H [2 * I + 2]. Each new number X is considered, and the update operation is required.
The Code is as follows:

If (x> H [0])
{
H [0] = X;
P = 0;
While (P <K)
{
Q = 2 * p + 1;
If (q> = K)
Break;
If (q <K-1) & (H [q + 1] <H [Q])
Q = q + 1;
If (H [Q] <H [p])
{
T = H [p];
H [p] = H [Q];
H [Q] = T;
P = Q;
}
Else
Break;
}
}

Original: http://blog.csdn.net/scottgly/article/details/6958227

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.