The K-largest number in the array can be obtained based on the fast sorting method. The steps are as follows:
1. Randomly select a fulcrum
2. Place the number greater than the pivot point to the left of the array. Place the number smaller than the pivot point to the right of the array. Place the pivot point to the center (left)
3. Set the length of the left part to L,
When K <L, Recursively search for the K-largest number in the left part.
When K> L, find the number (k-l) in the part recursively
When K = L, returns the Split points (that is, the fulcrum of the original) of the left and right parts, that is, the number of the required K
The code implementation of the above idea is as follows:
/** Calculate the maximum K Number of the array in linear time complexity ** Author: liuzhiwei ** data: 2011-08-07 **/# include "iostream" using namespace STD; // Based on the fast sorting idea, calculate the number k in array A. low and high are the start and end positions of arrays, respectively. // the time complexity is O (n ), n is the length of the array // 1 <= k <= N // If yes, returns the subscript of the k-th large number; otherwise, returns-1int selectk (int A [], int low, int high, int K) {If (k <= 0) Return-1; if (k> high-low + 1) Return-1; int lower = low + rand () % (high-low + 1); // select a fulcrum swap (A [low], a [low]); int M = low; int COUNT = 1; // a trip Traverse and place a large number on the left of the array for (INT I = low + 1; I <= high; ++ I) {if (a [I]> A [low]) {swap (A [++ m], a [I]); count ++; // count-1} swap (A [m], a [low]); // place the pivot point at the boundary of the left and right parts. If (count> K) {return selectk (A, low, m-1, k );} else if (count <k) {return selectk (a, m + 1, high, K-count);} else {return M ;}} int main (void) {int A [] = {5, 15, 5, 7, 9, 17,100, 3, 12, 10, 19, 18, 16, 10,100, 1}; int r = selectk ( , 0, sizeof (a)/sizeof (INT)-1, 5); cout <(r =-1? R: A [R]) <Endl; System ("pause"); Return 0 ;}
You can change it to the K decimal point in the array.
The complete code is as follows:
/** Linear time complexity: returns the K decimal number in the array ** Author: liuzhiwei ** data: 2011-08-07 **/# include "iostream" using namespace STD; // Based on the fast sorting idea, calculate the number of K smaller values in array A. low and high are the start and end positions of arrays respectively. // the time complexity is O (n ), n is the length of the array // 1 <= k <= N // If yes, returns the subscript of the nth decimal number. Otherwise, returns-1int selectk (int A [], int low, int high, int K) {If (k <= 0) Return-1; if (k> high-low + 1) Return-1; int lower = low + rand () % (high-low + 1); // select a fulcrum swap (A [low], a [low]); int M = low; int COUNT = 1; // a trip Traverse and place a small number to the left of the array for (INT I = low + 1; I <= high; ++ I) {if (a [I] <A [low]) {swap (A [++ m], a [I]); count ++; // The number of smaller than the Fulcrum is Count-1} swap (A [m], a [low]); // place the pivot point at the boundary of the left and right parts. If (k <count) {return selectk (A, low, m-1, k );} else if (k> count) {return selectk (a, m + 1, high, K-count);} else {return M ;}} int main (void) {int A [] = {5, 15, 5, 7, 9, 17,100, 3, 12, 10, 19, 18, 16, 10,100, 1}; int r = selectk (, 0, sizeof (a)/sizeof (INT)-1, 23); cout <(r =-1? R: A [R]) <Endl; System ("pause"); Return 0 ;}