Pagini recente » Cod sursa (job #48747) | Cod sursa (job #1587036) | Profil Federica361 | Cod sursa (job #2696844) | Cod sursa (job #1380211)
#include<fstream>
#include<ctime>
#include<algorithm>
using namespace std;
#define MAXN 3000005
int N,k,A[MAXN];
int pivotIndex,pivotValue,storeIndex;
ifstream cin("sdo.in");
ofstream cout("sdo.out");
int partitionare(int lower,int upper){
srand(time(NULL));
pivotIndex=lower+(rand()%(upper-lower+1));
// cout<<pivotIndex<<"\n";
pivotValue=A[pivotIndex];
swap(A[pivotIndex],A[upper]);
storeIndex=lower;
for(int i=1;i<upper;i++)
if(A[i]<pivotValue){
swap(A[i],A[storeIndex]);
storeIndex++;
}
swap(A[upper],A[storeIndex]);
if(storeIndex==k)
return A[storeIndex];
if(storeIndex<k)
return partitionare(storeIndex+1,upper);
else
return partitionare(lower,storeIndex-1);
}
int main(){
cin>>N>>k;
for(int i=0;i<N;i++)
cin>>A[i];
cout<<partitionare(1,N);
}