Pagini recente » Cod sursa (job #2039878) | Cod sursa (job #1757630) | Cod sursa (job #251263) | Cod sursa (job #1788141) | Cod sursa (job #1260505)
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#define Nmax 500005
using namespace std;
int a[Nmax],n;
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;
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;
srand(time(0));
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;
}