Cod sursa(job #655766)

Utilizator danirotRotaru Daniel danirot Data 3 ianuarie 2012 14:14:21
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include<stdio.h>
int N,a,i,v[500001];
void swap(int *x,int *y)
{int temp;
temp=*x;
*x=*y;
*y=temp;
}
void quicksort(int v[], int m, int n)
{
	int i = m,
		j = n,
		k = v[(m+n)>>1];

	while(i <= j)
	{
		while(v[i] < k) ++i;
		while(v[j] > k) --j;
		if(i <= j)
		{
			swap(&v[i], &v[j]);
			i++, j--;
		}
	}
	if(m < j) quicksort(v, m, j);
	if(n   > i) quicksort(v, i, n);
}

int main()
{
	freopen("algsort.in","r",stdin);
	freopen("algsort.out","w",stdout);

	scanf("%d",&N);
	for(i=0;i<N;i++) scanf("%d",&v[i]);
	quicksort(v, 0, (N-1));
	for(i=0;i<N;i++) printf("%d ",v[i]);
	return 0;
}