Input n integers and output the smallest k integers.

Source: Internet
Author: User
/*** Question: Enter n integers and output the smallest k of them. * For example, if you enter the 8 numbers 1, 2, 3, 4, 5, 6, 7, and 8, the minimum four digits are 1, 2, 3, and 4. * The simplest way to solve this problem is to sort the input n integers so that the number of K at the top is the minimum number of K. The time complexity of this approach is O (nlogn ). * The question does not require the number of K to be searched, or even the last n-k Number is ordered. In this case, why do we sort all N numbers? At this time, we thought of using selection or insertion sorting, that is, traversing the number of N. First, we saved the first number of K to the array with the size of K, and sorted the number of K, find the maximum number of K Kmax, k1 <k2, K3 <... <Kmax (Kmax is set to the maximum element in the array of k elements), O (k) is used, and then the number of n-k is further traversed. X is compared with Kmax, if x <Kmax, X replaces Kmax and sorts the array of k elements again. If X> Kmax, the array is not updated. In this way, the time used for each update or non-update of the array is O (k) or O (0). The total time complexity is averaged as follows: N * O (k) = O (N * K ). 3. Of course, a better solution is to maintain the maximum heap of k elements. The principle is consistent with the above 3rd solutions, that is, the maximum Heap Storage with a capacity of K is the minimum K number, k1 <k2 <... <Kmax (Kmax is set to the maximum element in the big top heap ). Traverse a series, N, and traverse an element x at a time. Compared with the heap top element, x <Kmax indicates that the heap is updated (logk used). Otherwise, the heap is not updated. In this case, the total time is O (n * logk ). This method is benefited from the fact that in the heap, the time complexity of operations such as search is logk (otherwise, as described in idea 2 above: Using arrays directly, you can also find the first K small elements, O (N * K )). */Public class mink {/***** @ Param krr * @ Param K * @ return */public static int [] Mink (INT krr [], int K) {int arr [] = new int [k]; for (INT I = 0; I <K; I ++) Arr [I] = krr [I]; buildheap (ARR); For (Int J = K; j <krr. length; j ++) {If (krr [J] <arr [0]) {arr [0] = krr [J]; maxheap (ARR, 1, k) ;}} return arr;}/*** create Max heap * @ Param arr */public static void buildheap (INT arr []) {int heapsize = arr. length; For (INT I = arr. length/2; I> 0; I --) Maxheap (ARR, I, heapsize );} /*** adjusted to the maximum heap * @ Param arr * @ Param I * @ Param heapsize */public static void maxheap (INT arr [], int I, int heapsize) {int largest = I; int left = 2 * I; int right = 2 * I + 1; if (left <= heapsize & arr [I-1] <arr [left-1]) largest = left; if (right <= heapsize & arr [Largest-1] <arr [Right-1]) largest = right; if (largest! = I) {int temp = arr [I-1]; arr [I-1] = arr [Largest-1]; arr [Largest-1] = temp; maxheap (ARR, largest, heapsize) ;}} public static void main (string [] ARGs) {int krr [] = {1, 3, 4, 2, 7, 8, 9, 10, 14,16 }; int arr [] = mink (krr, 4); For (INT I = 0; I <arr. length; I ++) system. out. println (ARR [I]);}

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.