The linear time of the algorithm introduction learning K-K elements + heap thought

Source: Internet
Author: User
Tags cmath

For the past, if I wanted to ask for a small element of k, or to ask for the first k elements, I would probably sort the elements first and then get them directly, but now I have a better idea.

First, the linear time to find the small element k
This algorithm is an algorithm based on the thought of divide and conquer. The specific ideas of division and treatment are as follows:
1. Decomposition: a[p,r] decomposition into a[p,q-1] and A[q+1,r] two parts, so that a[p,q-1] are less than a[q],a[q+1,r] are not less than a[q];
2. Solve: If A[Q] happens to be the K small element directly return, if the K-element falls in the first half of the interval to a[p,q-1] recursive lookup, otherwise to a[q+1,r] recursive lookup.
3. Merger: This issue does not need to be merged.
Its corresponding code is as follows:

intRandomziedselect (int*a,intPintRintK) {if(P==R)///   If there is only one element left in the current interval, then this element must be our request.        returnA[P];intQ=randomparatition (A,P,R);///   random partitioning function    intx=q-p+1;///   Find out the length between a[p,q]    if(x==k)///  A[q] happens to be the K small element        returnA[Q];if(x>k)///  x less than k description K small element between A[p,q-1]        returnRandomziedselect (a,p,q-1, k);Else  ///  x greater than K description K small element in A[q+1,r], and is the k-x small element of this interval        returnRandomziedselect (a,q+1, r,k-x);}

In fact, this process is very similar to the fast row, but why the time complexity of the fast line is O (NLGN), and the time complexity of the algorithm is only O (n)? The main reason is that the algorithm is processed every time as long as the decomposition of the half of the interval, and not as fast as the two sides to deal with. Of course, this is just a simple analysis, more specific mathematical analysis is not said here. In fact, we can also use the nature of the heap to find the K small element, as long as we build a minimum heap and then adjust k-1 times, so the time complexity is O (n) +o ((k-1) LGN).

A complete code is given below:

#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <ctime>#include <fstream>using namespace STD;intParatition (int*a,intPintR) {intKEY=A[R];inti=p-1; for(intj=p;j<r;j++)if(A[j]<key)            {i++;        Swap (a[i],a[j]); } Swap (a[i+1],a[r]);returni+1;}intRandomparatition (int*a,intPintR) {intX=rand ()% (r-p+1) +p;/// generate a random number between [P,r]Swap (a[x],a[r]);///Exchange A[x] and a[r], in fact, A[x] as the key value of the Division    returnParatition (a,p,r);}intRandomziedselect (int*a,intPintRintK) {if(P==R)/// If there is only one element left in the current interval, then this element must be our request.        returnA[P];intQ=randomparatition (A,P,R);/// random partitioning function    intx=q-p+1;/// Find out the length between a[p,q]    if(x==k)///a[q] happens to be the K small element        returnA[Q];if(x>k)///x less than k description K small element in A[p,q-1]        returnRandomziedselect (a,p,q-1, k);Else  ///x greater than k description K small element in A[q+1,r], and is the k-x small element of this interval        returnRandomziedselect (a,q+1, r,k-x);}intMain () {intb[ -]; Ifstream Fin ("Lkl.txt");intN,k;//cout<< "Please enter n,k:";fin>>n>>k;//cout<< "Please enter" <<n<< "elements:" <<endl;     for(intI=1; i<=n;i++) fin>>b[i];intAns=randomziedselect (b,1, n,k); Sort (b +1, b+n+1); for(intI=1; i<=n;i++)cout<<b[i]<<" ";cout<<endl;cout<<"section"<<k<<"The small element is:"<<ans<<endl;return 0;}

Two. Using the heap to find the top k elements
The idea of this algorithm is simple: if we ask for the first k elements of n elements, we will first create a minimum heap of the first k elements in the n elements, and then from k+1 ... N in turn, if an element is larger than the smallest element in the heap, we replace it with the smallest element in the heap and adjust the heap. After all the elements have been checked, the K elements in the heap are the top k elements of the n elements. Time complexity O (k) +o ((n-k) lgk).

The code is as follows

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <fstream>using namespace STD;#define MAXN/// Minimum heap adjustment functionvoidMinheadfly (int*a,intIintHeadsize) {intl=i*2, r=2*i+1;intlargest;if(a[i]>a[l]&&l<=headsize) largest=l;Elselargest=i;if(a[largest]>a[r]&&r<=headsize) Largest=r;if(largest!=i)        {swap (a[i],a[largest]);    Minheadfly (a,largest,headsize); }}/// Minimum heap build functionvoidMinheadbuild (int*a,intN) { for(inti=n/2; i>=1; i--) Minheadfly (a,i,n);}/// minimum heap sorting function, from large to small sortvoidMinheadsort (int*a,intHeadsize) {intK=headsize; for(inti=headsize;i>=2; i--) {Swap (a[i],a[1]);        k--; Minheadfly (A,1, k); }}/// ask for the first k elements of array BvoidPreKint*a,int*b,intNintk) {minheadbuild (a,k); for(inti=k+1; i<=n;i++)if(b[i]>a[1]) {a[1]=b[i]; Minheadfly (A,1, k); } minheadsort (A,k);cout<<"Front"<<k<<"The big element is:"<<endl; for(intI=1; i<=k;i++)cout<<a[i]<<" ";cout<<endl;}intA[MAXN],B[MAXN];intMain () {Ifstream fin ("Lkl.txt");intN,k;//cout<< "Please enter n,k:";fin>>n>>k;//cout<< "Please enter" <<n<< "elements:" <<endl;     for(intI=1; i<=n;i++) {fin>>b[i];if(i<=k) a[i]=b[i]; } prek (A,b,n,k);return 0;}

The linear time of the algorithm introduction learning K-K elements + heap thought

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.