Cod sursa(job #870274)

Utilizator pitradaPit-Rada Ionel-Vasile pitrada Data 3 februarie 2013 01:25:25
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<stdio.h>
int n,x[500002],i;

int sel(int st, int dr)
{
    int i,di,j,dj,aux;
    i=st; j=dr;
    di=+1; dj=0;
    while (i<j)
    {
        if(x[i]>x[j])
        {
            aux=x[i];
            x[i]=x[j];
            x[j]=aux;
            di=1-di;
            dj=1-dj;
        }
        i=i+di;
        j=j-dj;
    }
    return i;
}

void qs(int st, int dr)
{
    int m;
    if (st+1==dr)
    {
        if(x[st]>x[dr]){m=x[st]; x[st]=x[dr]; x[dr]=m;}
    }else
    if(st+1<dr)
    {
        m=sel(st,dr);
        qs(st,m-1);
        qs(m+1,dr);
    }
}

void tip()
{
    int i;
    for (i=1;i<=n;i++)
        printf("%d ",x[i]);
}

int main()
{
    freopen("algsort.in","rt",stdin);
    freopen("algsort.out","wt",stdout);
    scanf("%d",&n);
    for (i=1;i<=n;i++) scanf("%d",x+i);
    qs(1,n);
    tip();
    fclose(stdout);
    fclose(stdin);
    return 0;
}