Cod sursa(job #645762)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 10 decembrie 2011 14:42:52
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define file_in "algsort.in"
#define file_out "algsort.out"

int N,V[501000];

void quicksort(int l, int r){
	
	int i=l,j=r;
	int mij;
	mij=V[(l+r)/2];
	do
	{
		while((mij>V[i]) && (i<=N)) i++;
		while((mij<V[j]) && (j>=1)) j--;
		if (i<=j){
			swap(V[i],V[j]);
			i++;
			j--;
		}
	}
	while(i<=j);
	
	if (l<j) 
		quicksort(l,j);
	if (r>i)
		quicksort(i,r);
}
	

int main(){
	
	int i;
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d", &N);
	for (i=1;i<=N;++i)
		 scanf("%d", &V[i]);
	
	quicksort(1,N);
	for (i=1;i<=N;++i) printf("%d ", V[i]);
	
	return 0;
	
}