One algorithm per day (finding the smallest k elements (arrays ))

Source: Internet
Author: User

Question:

Input n integers and output k smallest numbers.

For example:


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.
Solution:
Of course, the simplest method is to sort all the n integers, but the time complexity of this method is particularly high.
Therefore, I used another k space in exchange for time. Each time a number is read from the input n integers. If the number of inserted elements in the array is less than k, the read integer is directly put into the array. Otherwise, the array with the length of k is full and cannot be inserted into the array. It can only be replaced.
If the integer to be read is smaller than the maximum value of k integers in the array, replace the maximum value with the integer to be read; if the integer to be read is larger than the maximum value of k integers in the array, the integer to be read cannot be one of the smallest k integers. This approach is equivalent to sorting k integers, so time complexity can be reduced to O (n + nlogk ). Generally, k is much smaller than n, so this method is better than the previous idea.

Run the following program in VS2010: The program in the example is correct. If there is any error, you may wish to specify it ..
[Cpp]
<Span style = "font-size: 18px;" >#include <iostream>
# Include <limits. h>
Using namespace std;
# Define N 100
# Define K 5
 
Void bubble_sort_k (int array [], int num)
{
Int I = 0;
Int j = 0;
// Int flag = 0;
Int temp = 0;
For (I = 0; I <num; I ++)
{
// Flag = 1;
For (j = I + 1; j <num; j ++)
{
If (array [I]> array [j])
{
Temp = array [j];
Array [j] = array [I];
Array [I] = temp;
// Flag = 0;
}
}
// If (flag = 1)
// Break;
}
Return;
}
 
Int main ()
{
Int array_n [N] = {INT_MAX };
// Int array_n [N] = {9, 8, 7, 6, 5, 4, 3, 2, 1 };
Int array_k [K] = {INT_MAX };
Int I = 0;
Int j = 0;
Int m = 0;
Int number = 0;
While (cin> I)
{
 
Array_n [number] = I;
Number ++;
}
// Number = 9;
For (j = 0; j <K; j ++)
{
Array_k [j] = array_n [j];
}

Bubble_sort_k (array_k, K );
 
For (I = K; I <number; I ++)
{
For (j = 0; j <K; j ++) // compare from the smallest element of the array
{
If (array_n [I] <array_k [j])
{
For (int n = K-1; n> j; n --) // After finding which element is smaller than this number, move the subsequent part to remove the largest element from the array.
{
Array_k [n] = array_k [n-1];
}
Array_k [j] = array_n [I];
Break;
}
}
}
For (m = 0; m <K; m ++)
{
Cout <array_k [m] <endl;
}
Return 0;
}
</Span>

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.