Pagini recente » Cod sursa (job #671042) | Cod sursa (job #239878)
Cod sursa(job #239878)
#include <stdio.h>
#define NX 500010
int N, A[NX], B[NX];
void merge_sort(int l, int r) {
int m = (l + r) >> 1, i, j, k;
if( l == r ) return;
merge_sort( l, m );
merge_sort( m + 1, r );
for( i = l, j = m+1, k = l; i <= m || j <= r; )
if( j > r || (i <= m && A[i] < A[j]) )
B[ k++ ] = A[ i++ ];
else
B[ k++ ] = A[ j++ ];
for( k = l; k <= r; k++ )
A[k] = B[k];
}
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 );
merge_sort( 1, N );
for( i = 1; i <= N; i++ )
printf( "%d ", A[i] );
return 0;
}