Pagini recente » Cod sursa (job #620262) | Cod sursa (job #2215654) | Cod sursa (job #1840007) | Cod sursa (job #1820329) | Cod sursa (job #1260555)
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#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(i<=j && 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 QSort(int st, int dr)
{
if(st>=dr) return;
int piv=st+rand()%(dr-st+1),poz;
swap(a[st],a[piv]);
poz=Pivot(st,dr);
QSort(st,poz-1); QSort(poz+1,dr);
}
int main()
{
int i;
freopen ("algsort.in","r",stdin);
freopen ("algsort.out","w",stdout);
srand(time(0));
scanf("%d", &n);
for(i=1;i<=n;++i) scanf("%d", &a[i]);
QSort(1,n);
for(i=1;i<=n;++i) printf("%d ", a[i]);
return 0;
}