Find the smallest number of K (four methods)

Source: Internet
Author: User

1 the smallest number of K is saved using a priority queue from large to small, and the remaining number after each fetch of the K number is compared to the top element of the heap, if it is smaller than the top element of the heap, the top element of the heap is deleted, and the element is inserted

voidTopK (intArr[],intNintk) {    if(k>N)return; Priority_queue<int>Q;  for(intI=0; i<n;++i) {if(Q.size () <k) Q.push (Arr[i]); Else        {            if(arr[i]<Q.top ())                {Q.pop ();            Q.push (Arr[i]); }        }    }     while(!Q.empty ()) {cout<<q.top () <<' ';    Q.pop (); } cout<<Endl;}

2 Use the Sort function of set, sort all the first k elements in order from large to small, take out the remaining elements compared to the first element, and if less than the first element, delete the first element and insert the current element.

voidTopK1 (intArr[],intNintk) {    Set<int,greater<int> >St; if(k>N)return;  for(intI=0; i<n;++i) {if(St.size () <k) St.insert (Arr[i]); Else{Auto ITER=St.begin (); if(*iter>Arr[i])                {st.erase (ITER);            St.insert (Arr[i]); }}} Auto iter=St.begin ();  while(iter!=St.end ()) {cout<<*iter++<<' '; } cout<<Endl;}

3 Use the Partition method to find the fixed position index of a number each time, where the element on the left is smaller than the element, and the element on the right is larger than the element. If index==k-1, then end the loop, find the index and the previous element is the smallest element of k, otherwise if the index

Greater than k-1, the element you need to find is on the left side of index, otherwise the element you need to find is on the right side of index. Loop to find until index==k-1.

intPartitionintArr[],intSinte) {    intpriov=Arr[e]; inti=s-1; intj=s;  for(; j<e;++j) {if(arr[j]<Priov) {            ++i;        Swap (arr[i],arr[j]); }    }    ++i;    Swap (arr[i],arr[e]); returni;}voidTopK2 (intArr[],intNintk) {    intIndex=partition (arr,0, N-1);  while(index!=k-1)    {        if(index<k-1) Index=partition (arr,index+1, N-1); ElseIndex=partition (arr,0, index-1); }     for(intI=0; i<k;++i) cout<<arr[i]<<' '; cout<<Endl;}

4 using the Select Sort function, find the minimum value in n elements to exchange with the first element, find the minor value from the n-1 element and swap it with the second element until the K element position is found.

voidTopK3 (intArr[],intNintk) {     for(intI=0; i<k;++i) {intmin=i; intj=i+1;  while(j<N) {if(arr[j]<arr[min]) min=J; ++J; }        if(min!=i) swap (arr[min],arr[i]); }     for(intI=0; i<k;++i) cout<<arr[i]<<' '; cout<<Endl;}

Find the smallest number of K (four methods)

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.