Cod sursa(job #1279379)

Utilizator TheNechizFMI Razvan Birisan TheNechiz Data 30 noiembrie 2014 10:40:55
Problema Sortare prin comparare Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.04 kb
# include <stdio.h>
# define InFile "algsort.in"
# define OutFile "algsort.out"
# define DIM 500000

int a[DIM],n;

void Citire()
{
    int i;
    scanf("%d",&n);
    for( i = 0 ; i < n ; ++i )
        scanf("%d",a+i);
}

void Afisare()
{
    int i;
    for( i = 0 ; i < n ; ++i )
        printf("%d ",*(a+i));
}

void Interclasare( int p, int m, int q )
{
    int i = p, j = m+1, k = 0;
    int b[DIM];

    while( i <= m && j <= q )
        if( a[i] < a[j] )
            b[k++] = a[i++];
        else
            b[k++] = a[j++];

    while( i <= m ) b[k++] = a[i++];
    while( j <= q ) b[k++] = a[j++];

    for( i = p ; i <= q ; ++i ) a[i] = b[i-p];
}

void MSort( int p , int q )
{
    if( q > p )
    {
        int m = (p+q)/2;
        MSort(p,m);
        MSort(m+1,q);
        Interclasare(p, m, q);
    }
}
int main()
{
    freopen(InFile,"r",stdin);
    freopen(OutFile,"w",stdout);

    Citire();
    MSort(0, n-1);
    Afisare();

    fclose(stdin);
    fclose(stdout);
    return 0;
}