Pagini recente » Cod sursa (job #1975948) | Cod sursa (job #2964208) | Cod sursa (job #1620375) | Cod sursa (job #896091) | Cod sursa (job #1512219)
#include <fstream>
using namespace std;
ifstream f("sdo.in");
ofstream g("sdo.out");
int v[3000000];
int main()
{
int n,i,left,right,aux,b,e,k,pivot;
f>>n>>k;
for(i=0; i<n; i++)
f>>v[i];
k--;
left=0;
right=n-1;
while(left<right)
{
pivot=v[(left+right)/2];
b=left;
e=right;
while(b<=e)
{
while(v[b]<pivot)
b++;
while(v[e]>pivot)
e--;
if(b<=e)
{
aux=v[b];
v[b]=v[e];
v[e]=aux;
b++;
e--;
}
}
if(k<=e)
right=e;
else
if(k>=b)
left=b;
else
left=right=k;
}
g<<v[k];
return 0;
}