Pagini recente » Cod sursa (job #1469188) | Istoria paginii runda/backsigeneratordetesteorelse | Cod sursa (job #1654304) | Cod sursa (job #2311988) | Cod sursa (job #1019443)
#include "stdio.h"
#include "stdlib.h"
int v[3000000],n,poz;
int qs(int s,int d)
{
int aux;
int i=s-1;
int j=d+1;
int pivot = v[s+rand()%(d-s+1)];
while(s<=d){
do{
++i;
}while(v[i]<pivot);
do{
--j;
}while(v[j]>pivot);
if(i<j){
aux=v[i];
v[i]=v[j];
v[j]=aux;
}else
return j;
}
}
void search(int s,int d){
if(s<d){
int pivot = qs(s,d);
if(pivot>=poz)
search(s,pivot);
else
search(pivot+1,d);
}
}
int main()
{
freopen("sdo.in", "r", stdin);
freopen("sdo.out", "w", stdout);
scanf("%d%d", &n,&poz);
int i;
for (i=1; i<=n; i++){
scanf("%d", &v[i]);
}
search(1,n);
printf("%d",v[poz]);
fclose(stdin);
fclose(stdout);
return 0;
}