Cod sursa(job #1065888)

Utilizator TheNechizFMI Razvan Birisan TheNechiz Data 23 decembrie 2013 19:47:57
Problema Sortare prin comparare Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.99 kb
# include <stdio.h>
# define InFile "algsort.in"
# define OutFile "algsort.out"
# define NMax 500001

int a[NMax],b[NMax],n;

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

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

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

    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+1];
}

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(1,n);
    Afisare();

    return 0;
}