Pagini recente » Cod sursa (job #469077) | Cod sursa (job #1967748) | Cod sursa (job #2455484) | Cod sursa (job #2118766) | Cod sursa (job #2856259)
#include <fstream>
#include <algorithm>
#include <time.h>
#define N 500002
using namespace std;
ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
int a[N], n;
void citire()
{
fin>>n;
for (int i=1; i<=n; ++i) fin>>a[i];
fin.close();
}
int pivotare(int st, int dr)
{
int p=rand()%(dr-st+1)+st;
swap(a[p], a[st]);
int i=st, j=dr, pasi=0, pasj=1;
while (i<j)
{
if (a[i]>a[j])
{
swap(a[i], a[j]);
pasi=!pasi; pasj=!pasj;
}
i+=pasi; j-=pasj;
}
return i;
}
void qsort(int st, int dr)
{
if (st<=dr)
{
int p=pivotare(st, dr);
qsort(st, p-1);
qsort(p+1, dr);
}
}
void afisare()
{
for(int i=1; i<=n; ++i) fout<<a[i]<<' ';
fout.close();
}
int main()
{
srand(time(NULL));
citire();
qsort(1, n);
afisare();
return 0;
}