Question 100: Question 5

Source: Internet
Author: User

Find the minimum k elements (array)
Question: Enter n integers and output the smallest k integers.
For example, if you enter the 8 numbers 1, 2, 3, 5, 6, 7, and 8, the minimum four digits are 1, 2, 3, and 4.

1. the most primitive Method

Insert sorting, and then output the minimum K

Source code:

# Include "stdio. H "/* Method for Finding the minimum k Number */void min (int A [], int N, int K) {int temp; For (INT I = 1; I <N; I ++) // insert sorting {for (Int J = 0; j <I; j ++) {if (a [J]> A [I]) {temp = A [J]; A [J] = A [I]; A [I] = temp ;}}}}

Int main () {int A [10] = {2, 1, 3, 4, 5, 7, 6, 8, 9}; // array int K = 5; // output a minimum of 5 min (A, 9, k );}

Disadvantage: high time complexity. O (N ^ 2 );

Ii. After simple Optimization

Sort by Using Quick Sort

 

Int partition (int A [], int I, Int J) {int temp = A [I]; // records the position of the lower axis while (I <j) {While (I <J & temp <= A [J]) // move a smaller value than the axis to the low-end j --; A [I] = A [J]; // The smaller one equals the greater one (the later I will increase) [this trip will not be moved after the large one moves to the front.] While (I <J & temp> = A [J]) // move a node larger than the axis to the high end. [move it from the front to the back, find a small one.] I ++; A [J] = A [I]; // The small (changed here) found above the [J] storage is equal to the large []} A [I] = temp; // return I;} void quicksort (int A [], int I, Int J) // fast sort {int P; if (I <j) {P = partition (A, I, j); quicksort (A, I P-1); quicksort (A, P + 1, J );}}

 

Analysis: although the time has been improved, it can still be optimized. O (nlogn)

3. Optimization again

Maintain an array of K sizes, traverse n elements at a time, and then obtain the minimum k elements.

Pseudocode:

1. Add the first K of n elements to the array. insert and sort the elements in the K array;

2. Starting from k + 1, each element is traversed. When maxk> A is compared with the largest (maxk) in the array, maxk is deleted and A is inserted into K. Until the end of the program.

Source code:

Void getmink_by_array (int A [], int N, int K) // maintain K arrays to obtain the smallest k elements {int Min [k]; // int I, j, temp, L; Min [0] = A [0]; // printf ("% 3d", Min [0]); for (I = 1; I <K; I ++) // insert a sort to maintain an array from small to large {Min [I] = A [I]; for (j = 0; j <I; j ++) {If (Min [I] <min [J]) {temp = min [J]; min [J] = min [I]; Min [I] = temp ;}}for (L = K; L <n; l ++) {if (a [l] <min [k-1]) {Min [k-1] = A [l]; for (j = 0; j <K; j ++) {If (Min [k-1] <min [J]) {temp = min [J]; Min [J] = min [k-1]; min [k-1] = temp ;}}}} 

5. It can also be optimized by replacing the maintenance of a K-size array with the maintenance of a K-size heap.

Void heapadjust (INT array [], int I, int nlength) // adjust the heap {int nchild; int ntemp; // assign a value to the node to be adjusted // start from the node to be adjusted until the node has no child node for (ntemp = array [I]; nlength> 2 * I + 1; I = nchild) // because the root node starts counting from 0, the left child is 2 * I + 1 {nchild = 2 * I + 1; // child node = 2 (parent node) + 1/* get a larger one if there are two subnodes in total * // nchild <The nLength-1 determines that there is no if (nchild <nLength-1 & array [nchild + 1]> array [nchild]) + + nchild;/* if a large subnode is greater than the parent node, adjust the subnode to the parent node */If (ntemp <array [nchild]) array [I] = array [nchild]; else break; // if this field is not added, an error will occur. The first will output the second array [nchild] = ntemp; // The child node is equal to the parent node} void heapsort (int A [], int length) {/* Initial heap */For (INT I = length/2-1; i> = 0; -- I) // adjust from the last non-leaf node (here I is the subscript) heapadjust (A, I, length); int temp; for (INT I = length-1; I> 0; -- I) // put the top of the heap to the end, then adjust the remaining element pairs {/* The first maximum element and the last one */temp = A [0]; A [0] = A [I]; A [I] = temp; heapadjust (A, 0, I); // adjust the heap (note that length = I is gradually reduced due to the heap )}} void getmink_by_heap (int A [], int N, int K) // maintain K arrays to obtain the smallest k elements {int Min [k]; // sort int I, j, temp, L; for (I = 0; I <K; I ++) in ascending order) // insert sorting maintains an array from small to large {Min [I] = A [I];} heapsort (Min, k); // heap sorting for (L = K; L <n; l ++) {if (a [l] <min [k-1]) {Min [k-1] = A [l]; // Let a [l] And Min [k-1] less than the maximum element in K heap swap heapadjust (Min, 0, k-1);} for (I = 0; I <K; I ++) printf ("% 3d", Min [I]);}

6. The code for the last four methods is as follows:

# Include "stdio. H "/* Method for Finding the minimum k Number */void min (int A [], int N, int K) {int temp; For (INT I = 1; I <N; I ++) // insert sorting {for (Int J = 0; j <I; j ++) {if (a [J]> A [I]) {temp = A [J]; A [J] = A [I]; A [I] = temp ;}}for (INT I = 0; I <K; I ++) printf ("% 3d", a [I]);} int partition (int A [], int I, Int J) {int temp = A [I]; // record the position of the lower axis while (I <j) {While (I <J & temp <= A [J]) // move a smaller value than the axis to the lower-end j --; A [I] = A [J]; // The smaller value is equal to the greater value (the later I will increase) [this trip will not be moved after the large one is moved to the front.] While (I <J & temp> = A [J]) // move a node larger than the axis to the high end. [locate and move it later.] I ++; A [J] = A [I]; // The small (changed here) found above the [J] storage is equal to the large []} A [I] = temp; // return I;} void quicksort (int A [], int I, Int J) // fast sort {int P; if (I <j) {P = partition (A, I, j); quicksort (A, I P-1); quicksort (A, P + 1, J );}} void getmink_by_array (int A [], int N, int K) // maintain K arrays to obtain the smallest k elements {int Min [k]; // int I, j, temp, L; Min [0] = A [0]; // printf ("% 3d", Min [0]); for (I = 1; I <K; I ++) // insert sort Maintenance 1 Array from small to large {Min [I] = A [I]; for (j = 0; j <I; j ++) {If (Min [I] <min [J]) {temp = min [J]; Min [J] = min [I]; min [I] = temp ;}}for (L = K; L <n; l ++) {if (a [l] <min [k-1]) {Min [k-1] = A [l]; for (j = 0; j <K; j ++) {If (Min [k-1] <min [J]) {temp = min [J]; Min [J] = min [k-1]; Min [k-1] = temp ;}}for (I = 0; I <K; I ++) printf ("% 3d", Min [I]);} void heapadjust (INT array [], int I, int nlength) // adjust the heap {int nchild; int ntemp; // value to the node to be adjusted // start from the node to be adjusted until the node has no children Subnode for (ntemp = array [I]; nlength> 2 * I + 1; I = nchild) // because the root node starts counting from 0, the left child is 2 * I + 1 {nchild = 2 * I + 1; // child node = 2 (parent node) + 1/* get a larger one if there are two subnodes in total * // nchild <The nLength-1 determines that there is no if (nchild <nLength-1 & array [nchild + 1]> array [nchild]) + + nchild;/* if a large subnode is greater than the parent node, adjust the subnode to the parent node */If (ntemp <array [nchild]) array [I] = array [nchild]; else break; // if this field is not added, an error will occur. The first will output the second array [nchild] = ntemp; // The child node is equal to the parent node} void heapsort (int A [], int length) {/* Initial heap */fo R (INT I = length/2-1; I> = 0; -- I) // adjust from the last non-leaf node (here I is a subscript) heapadjust (, i, length); int temp; For (INT I = length-1; I> 0; -- I) // put the top of the heap to the end, then adjust the remaining element pairs {/* The first maximum element and the last one */temp = A [0]; A [0] = A [I]; A [I] = temp; heapadjust (A, 0, I); // adjust the heap (note that length = I is gradually reduced due to the heap )}} void getmink_by_heap (int A [], int N, int K) // maintain K arrays to obtain the smallest k elements {int Min [k]; // sort int I, j, temp, L; for (I = 0; I <K; I ++) in ascending order) // insert sorting maintains an array from small to large {Min [I] = A [I];} heapsort (M In, k); // heap sorting for (L = K; L <n; l ++) {if (a [l] <min [k-1]) {Min [k-1] = A [l]; // Let a [l] less than the maximum element in K heap exchange with Min [k-1] heapadjust (Min, 0, k-1) ;}}for (I = 0; I <K; I ++) printf ("% 3d", Min [I]);} int main () {int A [10] = {2, 1, 3, 4, 5, 7, 6, 8, 9}; // array int K = 5; // output the minimum five printf ("********************** \ n"); printf ("1, after sorting is inserted, K minimum numbers \ n ") are obtained. printf (" 2. After fast sorting, K minimum numbers \ n ") is obtained. printf (" 3, maintain a K-size array and obtain K smallest numbers \ n "); printf (" 4, maintain a K-size heap and obtain K smallest numbers \ n "); printf ("*************** * *** \ N "); int I; while (1) {scanf (" % d ", & I); Switch (I) {Case 1: min (A, 9, k); printf ("\ n finished, please continue ...... \ N "); break; Case 2: quicksort (A, 0, 8); For (INT I = 0; I <K; I ++) printf (" % 3d ", A [I]); printf ("\ n execution is complete. Please continue ...... \ N "); break; Case 3: getmink_by_array (A, 9, k); printf (" \ n after execution is complete, please continue ...... \ N "); break; Case 4: getmink_by_heap (A, 9, k); printf (" \ n is finished. Please continue ...... \ N "); break; Case 5: break; Case 6: break; Case 7: break; case 8: break; Case 9: break ;}}}

 

 

 

 

 

 

 

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.