[Back To The watercress]: http://site.douban.com/196781/widget/forum/12161503/discussion/51632612/
[Question] 1. Set n numbers. Program to determine the number of K numbers.
[Idea 1] the idea of getting the K value in full sorting is straightforward. It can be done in fast or heap sorting. The complexity can be O (N * logn ); if you use base sorting, you can obtain a linearAlgorithm, But there is a limit on the element size.
[Idea 2] partial sorting. For example, if you use bubble or select sort, only bubble or select k elements, the time complexity is O (n * k). If K is small, this algorithm is still very good. the potato's practice is to determine the position of K in N to determine the bubble direction. (Speaking of Tudou, you do not need to work on algorithm questions. Who asked you to use qsort quickly and write it on your own !!) In addition, you can also perform some fast sorting. Each time, the fast sorting will get two arrays A, B, and array A, all of which are greater than the elements of array B, the array in which K is located determines which array is recursive. The average complexity of this algorithm is O (n * logk ).
[Idea 3] A solution provided in "the beauty of programming", first finding the maximum number of Max and min in N, and taking middle = min + (max-min) × 0. 5. In the max and min intervals, perform a binary search using the middle as the midpoint. For details, see page 137 of the beauty of programming.
[Idea 4] If n is large and 10 billion is large, it cannot be fully loaded into the memory. I did not take a closer look. For details, we can see page 139 of "the beauty of programming.
【Code] Includes a simple bubble version and some fast sorting algorithms. Continue to add later.
1 # Include <iostream> 2 # Include <vector> 3 # Include <cstdlib> 4 # Include <ctime> 5 Using Namespace STD; 6 7 Const Int N = 100 ; 8 Const Int K = 40 ; 9 10 Void Swap ( Int &, Int & B ){ 11 Int Temp = A; 12 A = B; 13 B = Temp; 14 } 15 16 Void Quicksort (vector < Int > & Arr, Int Low, Int High ){ 17 If (Low> High ){ 18 Return ; 19 } 20 Int I = low + 1 , J = High; 21 Int Middle = 0 ; 22 While ( True ){ 23 While (ARR [I]> arr [low] & I I; 24 While (ARR [J] <arr [low] & J> LOW )-- J; 25 If (I < J ){ 26 Swap (ARR [I], arr [J]); 27 } Else { 28 Break ; 29 } 30 } 31 Middle = J; 32 Swap (ARR [low], arr [Middle]); 33 If (K- 1 < Middle ){ 34 Quicksort (ARR, low, middle-1 ); 35 } Else If (K- 1 > Middle ){ 36 Quicksort (ARR, middle + 1 , High ); 37 } 38 } 39 40 Int Popselect (vector < Int > & ARR ){ 41 For ( Int I = 0 ; I <K; ++ I ){ 42 For ( Int J = arr. Size ()- 1 ; J> = I ;-- J ){ 43 If (ARR [J]> arr [J- 1 ]) { 44 Swap (ARR [J], arr [J- 1 ]); 45 } 46 } 47 } 48 Return Arr [k- 1 ]; 49 } 50 51 Int Main () 52 { 53 Srand (Time ( 0 )); 54 Vector < Int > Arr; 55 For (Int I = 0 ; I <n; ++ I ){ 56 Arr. push_back (RAND ()); 57 } 58 59 Quicksort (ARR, 0 , Arr. Size ()- 1 ); 60 Cout <arr [k- 1 ] <Endl <Endl; 61 62 Cout <popselect (ARR) < Endl; 63 // For (INT I = 0; I <n; ++ I ){ 64 // Cout <I <":" <arr [I] <Endl; 65 // } 66 System ( " Pause " ); 67 Return 0 ; 68 }