Pagini recente » Cod sursa (job #860731) | Cod sursa (job #1175831) | Cod sursa (job #1109872) | Cod sursa (job #1647876) | Cod sursa (job #442867)
Cod sursa(job #442867)
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
#include<fstream.h>
#define maxn 3000000
ifstream f ("sdo.in");
ofstream g ("sdo.out");
int n,Pozitie,Sir[maxn];
int Distribuire(int Left,int Right, int Pivot)
{
int Contor=Left-1;
for(int i=Left; i<=Right; i++)
{
if (Sir[i]<=Sir[Pivot])
Contor++;
}
Sir[Pivot]=Sir[Pivot]+Sir[Contor]-(Sir[Contor]=Sir[Pivot]);
for(int i=Left;i<Contor;i++)
{
if(Sir[i]>Sir[Contor])
{
int j=Contor+1;
while(Sir[j]>Sir[Contor])
{
j++;
}
Sir[i]=Sir[i]+Sir[j]-(Sir[j]=Sir[i]);
}
}
return Contor;
}
void StatOrd (int Left, int Right)
{
if(Left!=Right)
{
int h = Left + rand() % ( Right - Left );;
int Referinta=Distribuire(Left, Right, h);
if (Pozitie<=Referinta)
StatOrd(Left,Referinta);
else
{
//Pozitie -= Referinta;
StatOrd(Referinta+1,Right);
}
}
}
int main()
{
srand( time( 0 ) );
//random_poz = left + rand() % ( right - left );
f>>n>>Pozitie;
for(int i=1;i<=n;i++)
f>>Sir[i];
StatOrd(1,n);
g<<Sir[Pozitie];
return 0;
}