/* Minimum K count by rowandjj2014/8/9 */# include <stdio. h> # include <stdlib. h> int partition (INT arr [], int low, int high) {int val = arr [low]; while (low
[Suitable for processing massive data] Train of Thought 3:Yes
Create a data container with the size of K to store the minimum K numbers.Next, we will read a number from the input n integers each time. If the number in the container is less than K, we will directly put the number in the container, if the container already contains k numbers, we can only Replace the existing number instead of the new one. Find the maximum value of the existing K number, and compare the integer to be inserted with the maximum value. If the value to be inserted is smaller than the current maximum value, use this number to replace the current maximum value. If the value to be inserted is larger than the current maximum value, discard this number. Containers can use
Dading heap. The root of the Big Top heap is always the largest. We only need to compare the integer to be inserted with the heap top. If it is replaced, you only need to adjust the heapadjust function (which can be used in heap sorting ).
Code:
/* Minimum K count by rowandjj2014/8/10 */# include <stdio. h> # include <stdlib. h> void heapadjust (INT arr [], int start, int end) {If (ARR = NULL | start <0 | End <= 0 | Start> = end) {return;} int temp = arr [start]; int I = start * 2 + 1; while (I <= END) {if (I + 1 <= end & arr [I + 1]> arr [I]) {I ++;} If (temp> arr [I]) {break;} arr [start] = arr [I]; Start = I; I = I * 2 + 1;} arr [start] = temp;} bool getleastnumbers (INT input [], int N, int outpu T [], int K) {If (input = NULL | output = NULL | n <= 0 | K <= 0 | K> N) {return false;} int COUNT = 0; bool needbuildheap = true; For (INT I = 0; I <n; I ++) {If (count <K) {output [count ++] = input [I];} else {// The first time you need to build the entire heap if (needbuildheap) {for (Int J = K/2-1; j> = 0; j --) // heap, starting from the first non-leaf node {heapadjust (output, J, k-1);} needbuildheap = false ;} // after the large top heap is created, compare the integer of the current traversal with the size of the heap top element if (input [I]> = output [0]) // greater than or equal to the heap top element {continue; // Discard} // if it is smaller than the heap top element, you need to switch output [0] = input [I]; heapadjust (output, 0, k-1 ); // reset to the Big Top heap} return true;} // -------------------------------------------- // because of the requirements for sorting from small to large, void heapsort (INT arr [], int Len) {If (ARR = NULL | Len <= 1) {return;} int I; for (I = Len/2-1; I> = 0; I --) {heapadjust (ARR, I, len-1) ;}for (I = len-1; I> 0; I --) {int temp = arr [0]; arr [0] = arr [I]; arr [I] = temp; heapadjust (ARR, 0, I-1) ;}} int main (){ Int N, K; while (scanf ("% d", & N, & K )! = EOF) {If (n <= 0 | K <= 0) {continue;} int output [200000]; int * arr = (int *) malloc (sizeof (INT) * n); If (! ARR) {exit (-1) ;}int I; for (I = 0; I <n; I ++) {scanf ("% d ", arr + I);} getleastnumbers (ARR, N, output, k); heapsort (output, k); for (I = 0; I <K; I ++) {if (I = k-1) {printf ("% d \ n", output [I]);} else {printf ("% d ", output [I]) ;}} free (ARR) ;}return 0 ;}