Pagini recente » Cod sursa (job #2281220) | Cod sursa (job #1444823) | Cod sursa (job #1076999) | Cod sursa (job #1096172) | Cod sursa (job #2063443)
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int v[500005];
void rec(int st,int dr)
{
if(st==dr)
{
return;
}
else
{
int pozP=(rand()%(dr-st+1))+st;
swap(v[pozP],v[st]);
pozP=st;
for(int i=st+1;i<=dr;i++)
{
if(v[i]<v[pozP])
{
if(i-1==pozP)
{
swap(v[pozP],v[i]);
}
else
{
swap(v[pozP+1],v[i]);
swap(v[pozP],v[pozP+1]);
}
pozP++;
}
}
if(st<pozP-1)
rec(st,pozP-1);
if(pozP+1<dr)
rec(pozP+1,dr);
}
}
int main()
{
int n;
srand(time(NULL));
fin>>n;
for(int i=1;i<=n;i++)
fin>>v[i];
rec(1,n);
for(int i=1;i<=n;i++)
fout<<v[i]<<" ";
return 0;
}