Cod sursa(job #803926)

Utilizator wamfeverDobos Ionut wamfever Data 28 octombrie 2012 16:40:58
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include<cstdio>
#include<cstdlib>

#include<algorithm>

using namespace std;

#define NMax 500001

int v[NMax], a[NMax];
int n, k;

void inPut()
{
	freopen( "algsort.in", "r", stdin );
	
	scanf( "%d", &n );
	
	for(int i=1; i<=n; i++)
		scanf( "%d", &v[i] );
	
	fclose(stdin);
}

void ch( int A, int B )
{
	int AB = (A+B)/2;
	
	int x = A, y = AB+1;
	
	k = A-1;
	
	while ( x <= AB && y <= B )
	{
		if ( v[x] <= v[y] )
			a[++k] = v[x++];
		else
			a[++k] = v[y++];
	}
	
	while ( x <= AB )
		a[++k] = v[x++];
	while ( y <= B )
		a[++k] = v[y++];
	for(int i=A; i<=B; i++)
		v[i] = a[i];
}

void merge ( int a, int b )
{
	if( a == b )
		return;
	
	int ab = (a+b)/2;
	
	merge( a, ab );
	merge( ab+1, b );
	ch ( a, b );
}

void outPut()
{
	freopen( "algsort.out", "w", stdout );
	
	for(int i=1; i<=n; i++)
		printf ( "%d ", v[i] );
	
	printf ( "\n" );
	
	fclose(stdout);
}

int main()
{
	inPut();
	merge(1, n);
	outPut();
	return 0;
}