Pagini recente » Cod sursa (job #1084833) | Cod sursa (job #1442452) | Cod sursa (job #2904467) | Istoria paginii runda/againfminostress | Cod sursa (job #1260502)
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#define Nmax 500005
using namespace std;
int a[Nmax],n;
inline void Swap(int &x, int &y)
{
int aux;
aux=x; x=y; y=aux;
}
inline int Pivot(int st, int dr)
{
int i=st,j=dr;
while(i<=j)
{
while(i<=j && a[i]<=a[st]) ++i;
while(j>=i && a[j]>=a[st]) --j;
if(i<=j)
{
Swap(a[i],a[j]);
++i; --j;
}
}
Swap(a[st],a[i-1]);
return i-1;
}
inline void QuickSort(int st, int dr)
{
if(st>=dr) return;
srand(time(0));
int piv=rand()%(dr-st+1)+st,poz;
swap(a[st],a[piv]);
poz=Pivot(st,dr);
QuickSort(st,poz-1); QuickSort(poz+1,dr);
}
int main()
{
int i;
freopen ("algsort.in","r",stdin);
freopen ("algsort.out","w",stdout);
scanf("%d", &n);
for(i=1;i<=n;++i) scanf("%d", &a[i]);
QuickSort(1,n);
for(i=1;i<=n;++i) printf("%d ", a[i]);
return 0;
}