Pagini recente » Cod sursa (job #50359) | Cod sursa (job #1107914) | Cod sursa (job #1047618) | Cod sursa (job #253345) | Cod sursa (job #374253)
Cod sursa(job #374253)
# include <stdio.h>
# include <stdlib.h>
int a[3000005],i,n,aux,k,x,y;
void poz (int i,int j)
{
int ok=0;
while (i!=j)
{
if (a[i]>a[j])
{
if (ok==0)
ok=1;
else
ok=0;
aux=a[i];
a[i]=a[j];
a[j]=aux;
}
if (ok==0)
j--;
else
i++;
}
x=i;
}
int rez (int i,int j)
{
if (x==k)
return a[x];
else
{
y=(rand ()% (j-i))+i;
aux=a[i];
a[i]=a[y];
a[y]=aux;
poz (i,j);
if (x<k)
{
return rez (x+1,j);
}
else
{
return rez (i,x-1);
}
}
}
int main ()
{
freopen ("sdo.in","r",stdin);
freopen ("sdo.out","w",stdout);
scanf ("%i%i",&n,&k);
for (i=1;i<=n;i++)
scanf ("%i",&a[i]);
printf ("%i",rez (1,n));
return 0;
}