Cod sursa(job #671718)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 31 ianuarie 2012 19:47:35
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

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

#define nmax 501010

int N,V[nmax];

void qsort(int l, int r){
	
	int i=l;
	int j=r;
	int mij=V[(i+j)/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) qsort(l,j);
	if (i<r) qsort(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]);
	qsort(1,N);
	for (i=1;i<=N;++i)
		 printf("%d ", V[i]);
	
	return 0;
	
	
}