Find the minimum number of K

Source: Internet
Author: User

Enter n integers to find the smallest number of K

Solution 1: Need to modify the input array, based on partition fast sorting to do, time Complex fu O (N)

Analysis: Based on the k element of the array to adjust, so that the number of large than the number of K is placed on the right side of the array, so that the left of the array k is the smallest number of K

void Getlestnumber (int *input, int n, int *output, int k) {if (input = = NULL | | output = = NULL | | k > N | | n <= 0 | | K <= 0) return;int start = 0;int End = N-1;int index = Partition (input, n, start, end), while (Index! = k-1) {if (Ind Ex > k-1) {end = Index-1;index = Partition (input, n, start, end);} Else{start = index + 1;index = Partition (input, n, start, end);}} for (int i = 0; i < K; ++i) {output[i] = Input[i];}}

Solution Two:

Analysis: First create a data container of size k to store the smallest number of k, and then each time from the input n integer read into a number, if the container is less than the number of K is directly put, if greater than the container to full, at this time find the maximum value in the container, and then take the input value and the maximum value, is replaced directly.

The structure of the maximum heap can get the maximum value at O (1) at a time, but requires O (LOGK) to complete the deletion and insertion

The red and black trees are the same, but the code is simply a lot

Void getlestnumber (int *input, int n, int *output, int k) {         if  (input == null | |  output == null | |  k > n | |  n <= 0 | |  k <= 0)           return; int  start = 0;      int end = n - 1;     int index = partition (input, n, start, end);         while  (index != k - 1)       {            if  (index > k -  1)                {                    end = index - 1;                     index =  Partition (input, n, start, end);             }           else                 {                    start = index +  1;                   index = partition (input, n, start, end);             }   }   for  (int i = 0;  i < k;&nbSp;++i)       {            output[i] = input[i];       }} solution two:     Analysis: First create a data container of size k to store the smallest number of k, and then each time from the input n integer read into a number, if the container is less than the number of K is directly put, if greater than the container to full, at this time find the maximum value in the container, and then take the input value and the maximum value, is replaced directly.      maximum heap structure can get the maximum value at O (1) at a time, but requires O (LOGK) to complete the removal and insertion of const int n = 1000;const  int k = 10;void adjustdown (int *a, int size, int parent) {  int child =  (parent - 1)  / 2;        while  (child < size)      {            if  (Child+1<size&&a[child]>a[child + 1])                        child++;            if  (A[child]>a[parent])           {                    swap (A[child], a[parent]);                   parent =  child;                      child =  (parent - 1)  / 2;            }            else                  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BREAK;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}}VOID&NBSP;GETTOPK (Int*a) {  assert (k  < n);   int top[k]; for  (int i = 0; i <  k; i++)       {            top[i] = a[i];      }   for  (int  i =  (k - 2)  / 2; i >= 0; i++)    {            adjustdown (top, k, i);       }   for  (int i = n-k-1; i < n; i++ )   {           if  (a[i]>top[0])           {                    top[0] = a[i];         &nbSp;             adjustdown (top, k,  0);               }   }}


This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1786120

Find the minimum number of K

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.