#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int partion(vector<int>&v,int low,int high){
int key = v[low];
while(low < high){
while(low<high && v[high]>= key){
high--;
}
if(low<high){
v[low++] = v[high];
}
while(low<high && v[low]<= key){
low++;
}
if(low<high){
v[high--] = v[low];
}
}
v[low] = key;
return low;
}
void getKnum(vector<int>&v,int k){
if (v.size() == 0 || k>v.size())
{
return ;
}
int start = 0;
int end = v.size()-1;
int index = partion(v,start,end);
while (index != k-1)
{
if (index > k-1)
{
end = index - 1;
index = partion(v,start,end);
}
else
{
start = index + 1;
index = partion(v,start,end);
}
}
sort(v.begin(),v.begin()+k);
for (int i=0;i<k;i++)
{
cout<<v[i]<<" ";
}
}
int main(){
vector<int>v;
int n,t;
while(cin>>t){
v.push_back(t);
if (getchar() == '\n')
{
break;
}
}
int k = v[v.size()-1];
v.pop_back();
getKnum(v, k);
return 0;
}
数组的最小K个数字,按序输出
最新推荐文章于 2021-06-02 22:27:32 发布