Analysis of the algorithm for the number of K-large in the search sequence

Source: Internet
Author: User

problem Description: Given a series of numbers {A1,a2,..., an}, these numbers are unordered and now ask for the number of k large.

To see this problem, the first thought is to sort first, then directly output the number of K, and then get a sort-based algorithm

Algorithm one:

#include <iostream>
#include <algorithm>
using namespace Std;
BOOL CMP (int a, int b) {return a > B;}
int main () {
int k;
int a[9] = {6, 5, 9, 8, 2, 1, 7, 3, 4};
cout << "Input K:";
Cin >> K;
Sort (A, a + 9, CMP);
cout << a[k-1] << Endl;
return 0;
}

Algorithm time is mainly in the sort, so the algorithm complexity is O (NLOGN);

Algorithm two:

The second algorithm is based on the decomposition method, the problem is the number of K, if I will be the number of all of this series will certainly do too much work, for example, I just find out the K big, I can first find out 1 big, 2nd big .... K Large, at the end, at this time O (kn), when K is less time the algorithm complexity is 0 (n), if the k is the function of n when the algorithm complexity O (n2), the best case is the k=1 and K=n, the worst case is K=n/2,o (n) =<f (n) <=o (n2/ 2); The specific code is as follows:

#include <iostream>
using namespace Std;
#include <cmath>
#define M int (POW ( -2,31)); /* Assign M to the smallest integer */
int find_kth (int a[], int n, int k);
int main () {
int k;
int a[9] = {6, 5, 9, 8, 2, 1, 7, 3, 4};
cout << "Input K:";
while (Cin >> K) {
cout << find_kth (A, 9, k) << Endl;
}
return 0;
}
int find_kth (int a[], int n, int k) {
int i,j, redex=0, Newmax = a[0];
for (i = 0; i < K; i++) {/* * each time the Daleitai method is used to find the maximum value, the value is m*/
for (i = 0; i < n; i++)
if (A[i]>newmax) {
Newmax = A[i];
Redex = i;
}
A[redex] = m;
}
return Newmax;
}

Algorithm two has limitations, the complexity depends on the K value, the following or the use of decomposition-based thinking, we need to ask for the number of k large, so you can use the idea of fast sorting, arbitrarily select a number key to decompose this sequence number into S1 and S2 two sequence, where the number of S1 is all less than key, While the number in S2 is all greater than or equal to key (key is the smallest number in S2), set S1 and s2 the number of elements is |s1| and |s2|, if k==|s2|, that the number of k large is exactly key, if k<|s2|, then the problem is converted to |s2| in the number of K, If k>|s2|, then the problem is converted to S1 in the k-|s2| large number, so that all the process can be achieved by recursion, so there is algorithm three.

algorithm three:
#include <iostream>
using namespace std;
int find_kth (int *a,int left,int right,int k);
Int main () {
int k;
int a[9] = {6,5,9,8,2,1,7,3,4};
cout << "Input K:";
Cin >> K;
cout << find_kth (A, 0, 8, k) << Endl;/* Output K-Large number */
return 0;
}
int find_kth (int *a,int left,int right, int k) {
int key = A[left];/* Set key value */
int low = Left,high=right;
while (High>low) {/* is similar to quick sort */
while (A[high] >= key&&high>low)
high--;
A[low] = A[high];
while (A[low] <= key&&high>low)
a[low++];
A[high] = A[low];
}/* loop jumps out must have high=low*/
A[low] = key;
int r = Right-low + 1; /* Number of right-side elements */
if (k== R)
return A[low];//////////////////////////////A[low] for K-value
Else if (K < R)
return find_kth A, low+1 /k-Large value on A[low] the right of the K-value
Else
return find_kth (A, left, low-1, k-r);/* The value of the first a[low in the right sequence of the K-large value in r-k] */
}

In fact, the STL of C + + has already provided us with the function nth_element, including the header file #include<iostream>. nth_element has four parameters, the first and third are the addresses of begin and end, Indicates that the search scope is the number in the interval [begin,end], the second parameter is the address of the number of K to find, the fourth parameter is the CMP function, and the function can be considered void element (Int*begin,int*nth,int*end,boo (*PDRF) ( int a,int b));

#include <iostream>
#include <algorithm>
using namespace Std;
BOOL CMP (int a, int b) {
return a > B;
}
int main () {
int k;
int a[9] = {6, 5, 9, 8, 2, 1, 7, 3, 4};
cout << "Input K:";
while (Cin >> K) {
Nth_element (A, A + k-1,a+9, CMP); Place the number k large in the k-1 position
cout << a[k-1] << Endl;
}
return 0;
}

Analysis of the algorithm for the number of K-large in the search sequence

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.