Obtain the maximum number of k elements from n elements.

Source: Internet
Author: User

Requirement: n elements are used to retrieve k elements with a maximum size (small). Describe the concept and algorithm.

Note: Each method has its applicability. For example, some methods are applicable when n is large.

Method 1: Sort the elements first, and then remove the largest k elements. You can use basic direct insertion sorting, Bubble sorting, and fast sorting and heap sorting methods. In general, fast sorting is required, because this method is the best in all.

Quick Sort version 1:This method is proposed in the introduction to algorithms. The comparison idea is: Compare the initial element with the selected element (the rightmost element). If it is larger than the selected element, it is not processed. If it is smaller than the selected element, it will be replaced.

Int partition (int A [], int P, int R) // non-descending sorting method <br/>{< br/> int x = A [R]; <br/> int I = p-1, mid; <br/> for (Int J = P; j <r; j ++) <br/> {<br/> if (a [J] <= x) // search for data smaller than X, after finding it, it will switch forward <br/>{< br/> I = I + 1; <br/> if (I! = J) // if they are not equal, they need to be exchanged. In the introduction to algorithms, they are directly exchanged. Here we can optimize them. <br/> {<br/> swap (& A [I], & A [J]); <br/>}< br/> swap (& A [I + 1], & A [R]); <br/> return I + 1; <br/>}< br/> void quicksort (int A [], int P, int R) <br/>{< br/> int Q; <br/> If (P <r) <br/>{< br/> q = partition (A, P, r); // recursive call after obtaining q <br/> quicksort (A, P, q-1); <br/> quicksort (A, q + 1, R ); <br/>}< br/>} 

Quick Sort Version 2: The general method, which is compared from both sides.

Int partition1 (int A [], int low, int high) <br/>{< br/> int lower tkey; <br/> lower tkey = A [low]; // pivot record keyword <br/> while (low <pigh) // The two ends of the table are alternately scanned in the middle <br/>{< br/> while (low <High & A [High]> = duplicate tkey) <br/> -- high; <br/> A [low] = A [High]; // move a lower position than the hub <br/> while (low <High & A [low] <= foreign tkey) <br/> + low; <br/> A [High] = A [low]; // move a value greater than the hub to a high position <br/>}< br/> A [High] = Beijing tkey; // pivot record in place <br/> return high; // return pivot location <br/>}< br/> void quicksort (int A [], int P, int R) <br/> {<br/> int Q; <br/> If (P <r) <br/> {<br/> q = partition1 (A, P, r); // recursive call after obtaining q <br/> quicksort (A, P, q-1); <br/> quicksort (A, q + 1, R ); <br/>}< br/>} 

Method 2: Use a minimum heap to store K numbers, and traverse the array from k + 1. If it is greater than the heap top element, replace the heap top element and re-adjust the heap. The maximum k elements are retained.

Method 3: Use hash to save the number of times the element Si appears in the array, and use the counting sorting method to obtain the first K number in the linear scanning process from large to small, average time complexity O (N)

Method 4: the beauty of Programming

First, find the maximum K number. The time complexity can be O (n). The specific method is as follows:

Select a random number from N number and scan it again. If n is greater than N, place it on the right, r elements, and N on the left. If a: L =
K-1 returns n
B: L> the K-1 continues the previous operation in the L element.
C: L <The K-1 continues the previous operation in the r element. B, C only needs to execute one item at a time, so the average complexity is: O (N + n/2 + N/4...) = O (2n) = O (N)

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.