Pagini recente » Cod sursa (job #1515660) | Cod sursa (job #1881232) | Cod sursa (job #1355929) | Cod sursa (job #2174368) | Cod sursa (job #2983579)
#include<bits/stdc++.h>
using namespace std;
ifstream in("sdo.in");
ofstream out("sdo.out");
vector<int> vec;
int n,k;
void citire(){
int aux;
in>>n>>k;
for(int i=1;i<=n;++i){
in>>aux;
vec.push_back(aux);
}
}
int part(int l,int h){
int b=l,e=h;
srand((unsigned) time(nullptr));
int z=b+rand()%(e-b+1);
int pivot=vec[z];
do{
do
b++;
while(vec[b]>pivot);
do
e--;
while(vec[e]<=pivot);
if(b<e)
swap(vec[e],vec[b]);
}while(b<e);
return z;
}
void qs(int be,int en){
if(be>=en)
return ;
int pivot_ind=part(be,en);
if(pivot_ind==k)
return;
if(pivot_ind>k)
qs(be,pivot_ind-1);
if(pivot_ind<k)
qs(pivot_ind+1,en);
}
int main(){
citire();
qs(0,n-1);
out<<vec[k-1];
}