Cod sursa(job #668305)

Utilizator toniobFMI - Barbalau Antonio toniob Data 24 ianuarie 2012 17:58:03
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
using namespace std;

ifstream in ("algsort.in");
ofstream out ("algsort.out");

int v[500006],n;

void quickSort (int v[],int l,int r) {
	if (l>=r) return;
	
	int pivot = v[(rand()%(r-l))+l],i=l,j=r,aux;
	
	while (i<j) {
		while (v[i] < pivot) ++i;
		while (v[j] > pivot) --j;
		if (i <= j) {
			aux=v[i];
			v[i]=v[j];
			v[j]=aux;
			++i;
			--j;
		}
	}
	
	quickSort(v,l,j);
	quickSort(v,i,r);
}
	

void citire () {
	in >> n;
	for (int i = 1;i <= n; ++i) {
		in >> v[i];
	}
}

void afisare () {
	for (int i = 1;i <= n; ++i) {
		out << v[i] << " ";
	}
}

int main () {
	citire ();
	
	quickSort(v,1,n);
	
	afisare ();
	
	return 0;
}