Calculates the maximum K value of an integer array by bubble sort.

Source: Internet
Author: User

Let's take a look at the best method. This is only improved using the Bubble sorting method.

 

# Include <iostream>
Using namespace STD;

// Calculate the maximum K value
// The idea is to use the Bubble sorting method. A major drawback is that it changes the order of the original array.
Int querykmax (int A [], int Len, int K)
{
Int COUNT = 0;
Int temp = 0;
Bool endcir = true;

For (INT I = 0; (I <Len) & (count <= k) & endcir; I ++)
{
Endcir = false;
For (Int J = 0; j <len-I; j ++)
{
If (A [J]> A [J + 1])
{
Temp = A [J];
A [J] = A [J + 1];
A [J + 1] = temp;
Endcir = true;
}

}
Count ++;
}

Return a [Len-count + 1];
}

Void main ()
{
Int arr [10] = };

Int Len = 10;
Int K = 4;

Cout <"The K max value is" <querykmax (ARR, Len, k) <Endl;

For (INT I = 0; I <Len; I ++)
{
Cout <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.