Cod sursa(job #2063443)
| Utilizator | Data | 11 noiembrie 2017 11:37:43 | |
|---|---|---|---|
| Problema | Sortare prin comparare | Scor | 60 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.16 kb |
#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;
}
