#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
//******************************************************
void Swap(int *a,int *b){
int tmp=*a;
*a=*b;
*b=tmp;
}
int RandomInRange(int start,int end){
srand(time(NULL));
return start+rand()%(end-start+1);
}
int partition(int *number,int length,int start,int end){
//if(number==NULL||length<=0||start<0||end>length-1)
//throw new std::exception("Invalid Input.");
int index=RandomInRange(start,end);
Swap(&number[index],&number[end]);
int flag=start;
for(int i=start;i<end;++i){
if(number[i]<number[end]){
if(i!=flag)
Swap(&number[i],&number[flag]);
flag++;
}
}
Swap(&number[flag],&number[end]);
return flag;
}
//*******************************************************
//solution1:use partition to narrow
void GetLeastNum(int *input,int n,int *output,int k){
if(!input||!output||n<=0||k<=0||n<k)
return;
if(n==k){
for(int i=0;i<k;++i){
output[i]=input[i];
}
return;
}
int start=0,end=n-1;
int index=partition(input,n,start,end);
while(index!=k){
if(index>k){
end=index-1;
index=partition(input,n,start,end);
}
else{
start=index+1;
index=partition(input,n,start,end);
}
}
for(int i=0;i<k;++i){
output[i]=input[i];
}
}
//solution2:to be continued
int main(){
int array[]={5,7,6,8,2,4,1,9,3};
int k;
cin>>k;
int *least=new int [k];
GetLeastNum(array,sizeof(array)/sizeof(*array),least,k);
for(int i=0;i<k;++i){
cout<<least[i]<<' ';
}
delete []least;
}