Cod sursa(job #956445)

Utilizator CaligulaGAIVS IVLIVS CAESAR AVGVSTVS GERMANICVS Caligula Data 3 iunie 2013 10:18:35
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include<cstdio>
using namespace std;
int n,a[500005];
void schimb(int &x, int &y)
{
    int aux=x;
    x=y; y=aux;
}
void qsort(int st, int dr)
{
    int i=st,j=dr,piv=a[(st+dr)/2];
    while (i<=j)
        {
            while (a[i]<piv) ++i;
            while (a[j]>piv) --j;
            if (i<=j) schimb(a[i],a[j]), ++i, --j;
        }
    if (i<dr) qsort(i,dr);
    if (st<j) qsort(st,j);
}
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]);
    qsort(1,n);
    for (i=1;i<=n;++i)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}