Divide and conquer the law to find the k small element (vc ++)

Source: Internet
Author: User

Algorithm:

Calculate the k-small element in a column and use the grouping policy for Recursive solution.

First, specify a random number. Here, we specify the first number as the smallest k element and record it as randK. Compare the other numbers in the array with the findK, and place them smaller than the findK on the left, big put on the right, if randK to the left of the number of elements for the K-1, it means that findK is the element you want to find, if the number of elements on the left> K-1, it indicates that the element you are looking for is in the number on the left, continue to use the same method to search in the number on the left, if the number of elements on the left <K-1, it indicates that the element you are looking for is in the number on the right, and the same method will be used to search for it in the number on the right...

Code:

# Include <iostream. h>

# Include <time. h>

# Include <stdlib. h>

# Define Array_max 200

 

Void swap (int & a, int & B ){

Int temp;

Temp =;

A = B;

B = temp;

}

Void out (int a [], int n ){

For (int I = 0; I <n; I ++ ){

Cout <a [I] <"";

If (I % 8 = 0 & I! = 0)

Cout <endl;

}

Cout <endl;

}

Int findP (int a [], int oP, int R) {// oP is where the random element starts to 0, left oP, Right R

Int randK = a [oP]; // you can specify a random number.

Int iL = oP + 1;

Int iR = R;

While (true ){

While (a [iL] <randK) {iL ++ ;}

While (a [iR]> = randK) {iR --;}

If (iL> = iR) break;

Swap (a [iL], a [iR]);

}

Swap (a [oP], a [iR]);

Return iR;

}

Int findK (int a [], int oP, int n, int k) {// n indicates the number of elements to be sorted.

Int find_k;

Int copy_k;

Find_k = findP (a, oP, n); // points to the minimum value of the element, and oP points to the maximum value of the element.

Copy_k = find_k-oP;

If (k = copy_k)

Return a [find_k];

If (k> copy_k)

Return findK (a, find_k + 1, n, k-copy_k-1 );

If (k <copy_k)

Return findK (a, oP, find_k-1, k );

Else return 0;

}

Void main (){

Int n;

Int k;

Begin:

Cout <"Please input the length of your Array :";

Cin> n;

Cout <endl;

Int * a = new int [n];

Srand (unsigned) time (NULL); // generate a random number

For (int I = 0; I <n; I ++ ){

A [I] = rand () %100;

}

Cout <"The Random_Array_element is:" <endl;

Out (a, n );

Cout <"Pleae input the K :";

Cin> k;

If (k> n) goto Begin; // input error, re-Input

Else

Cout <"The element you find now is:" <findK (a, 0, n-1, k-1) <endl;

}

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.