Title Description Enter n integers to find the smallest number of K. For example, enter 4,5,1,6,2,7,3,8 these 8 numbers, then the smallest 4 number is 1,2,3,4,.
//Heap Sort#include <iostream>#include<algorithm>#include<vector>using namespacestd;classsolution{ Public: voidHeapadjust (vector<int> &a,intSintN) {intRc=A[s]; for(intj=2*s+1; j<=n-1; j=j*2+1) { if(j<n-1&&a[j]>a[j+1]) J++; if(rc<=A[j]) Break; Else{A[s]=A[j]; S=J; }} A[s]=RC; } voidCreatheap (vector<int> &a,intN) { for(inti=n/2-1; i>=0; i--) {heapadjust (a,i,n); }} vector<int> Getleastnumbers_solution (vector<int> Input,intk) {vector<int>ans; if(input.size () = =0|| k==0|| K>input.size ())returnans; intn=input.size (); Creatheap (Input,n); for(inti=n-1; I>0; i--) { intx=input[0]; input[0]=Input[i]; Input[i]=x; Heapadjust (Input,0, i); } for(intI=input.size ()-1; i>=0&&k>0; i--) {ans.push_back (input[i]); K--; } returnans; }};intMain () {solution S; intk=4; Vector<int>ans; Vector<int>input; Input.push_back (4); Input.push_back (5); Input.push_back (1); Input.push_back (6); Input.push_back (2); Input.push_back (7); Input.push_back (3); Input.push_back (8); Ans=s.getleastnumbers_solution (input,k); for(intI=0; I<ans.size (); i++) {cout<<Ans[i]; } return 0;}
//bfprt#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespacestd;classSolution { Public: Vector<int> Getleastnumbers_solution (vector<int> Input,intk) {vector<int>ans; if(input.size () = =0|| k==0|| K>input.size ()) { returnans; } for(intI=0; I<=BFPRT (Input,0, Input.size ()-1, k); i++) {ans.push_back (input[i]); } returnans; } /*Inserts a sort, returns the middle digit subscript*/ intInsertsort (vector<int> &array,intLeftintRight ) { inttemp; intJ; for(inti = left +1; I <= right; i++) {Temp=Array[i]; J= i-1; while(j >= left && Array[j] >temp) Array[j+1] = array[j--]; Array[j+1] =temp; } return((Right-left) >>1) +Left ; } /*returns The median subscript for the median number of digits*/ intGetpivotindex (vector<int> &array,intLeftintRight ) { if(Right-left <5) returninsertsort (array, left, right); intSub_right = left-1; for(inti = left; i +4<= right; i + =5) { intindex = insertsort (array, I, i +4);//Find the subscript for the median of five elementsSwap (Array[++sub_right], array[index]);//On the left, in turn } returnBfprt (array, left, Sub_right, (Sub_right-left +1) >>1) +1); } /*Using the median median of the subscript to divide, return to the dividing line subscript*/ intPartition (vector<int> &array,intLeftintRightintPivot_index) {Swap (Array[pivot_index], array[right]); //Place the datum at the end intDivide_index = left;//the dividing line of tracking Division for(inti = left; I < right; i++) { if(Array[i] <Array[right]) swap (Array[divide_index+ +], array[i]);//smaller than the benchmark, on the left.} swap (Array[divide_index], array[right]); //Finally, return the benchmark . returnDivide_index; } intBFPRT (vector<int> &array,intLeftintRightConst int&k) {intPivot_index = Getpivotindex (array, left, right);//to get The median of the middle-digit subscript intDivide_index = Partition (array, left, right, Pivot_index);//to divide and return to a dividing boundary intnum = Divide_index-left +1; if(num = =k)returnDivide_index; Else if(Num >k)returnBfprt (array, left, Divide_index-1, K); Else returnBfprt (Array, Divide_index +1, right, K-num); }};intMain () {solution S; intk=4; Vector<int>ans; Vector<int>input; Input.push_back (4); Input.push_back (5); Input.push_back (1); Input.push_back (6); Input.push_back (2); Input.push_back (7); Input.push_back (3); Input.push_back (8); Ans=s.getleastnumbers_solution (input,k); for(intI=0; I<ans.size (); i++) {cout<<Ans[i]; } return 0;}
Minimum number of k C + + (bfprt, heap sort)