Written question: The number of the first n Large (small) in order to find the chaos. The best way I can think of.

Source: Internet
Author: User

#include <iostream>using namespace std;//of course the same is true with fast sorting. Class Heap{public:heap () {}void Insert (int a[],int len,int N) {data = new int[n];size = n;for (int i=0;i<n;i++) {Data[i]=a [i];} int n = n/2;while (n>=0) {setval (data,n); n--;} for (int i=n;i<len;i++) {if (Data[0]>a[i]) {data[0]=a[i]; Setval (data,0);}}} void Setval (int a[],int n) {int i = N;int j = i*2+1;while (j<size) {if (j+1<size && data[j]<data[j+1]) j=j+1; if (Data[i]<data[j]) {int temp = Data[i];d ata[i] = data[j];d ata[j] = temp;} i = J;j = I*2+1;}} int Getnum () {return data[0];} ~heap () {if (data) delete []data;} Private:int *data;int size;}; int find_n_min (int a[],int n,int val) {heap hp;//I use the large heap to solve, though it is a large heap, I will insert the smaller number,//And then in this group of smaller numbers inside, according to the large heap of ascending way, Let the first subscript position Save the value I need to find. Hp. Insert (a,n,val); return HP. Getnum ();} int main () {int a[]={2,3,4,5,6,11,2,3,3,4,4,5,5,6,0,0,0};cout<<find_n_min (a,sizeof (a)/sizeof (int), 4) < <endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Written question: The number of the first n Large (small) in order to find the chaos. The best way I can think of.

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.