Cod sursa(job #803519)

Utilizator vld7Campeanu Vlad vld7 Data 27 octombrie 2012 18:46:54
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>

#define maxN 500005

using namespace std;

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

int n, a[maxN];

void read()
{
	f >> n;
	for (int i = 1; i <= n; i++)
		f >> a[i];
}

void quicksort(int st, int dr)
{
	int i = st, j = dr, pivot;
	
	pivot = a[(i + j) / 2];
	while (i <= j) {
		while (a[i] < pivot)
			i++;
		while (a[j] > pivot)
			j--;
		
		if (i <= j) {
			int aux = a[i];
			a[i] = a[j];
			a[j] = aux;
			i++;
			j--;
		}
	}
	
	if (i < dr)
		quicksort(i, dr);
	if (st < j)
		quicksort(st, j);
}

int main()
{
	read();
	quicksort(1, n);
	
	for (int i = 1; i <= n; i++)
		g << a[i] << " ";
	
	f.close();
	g.close();
	
	return 0;
}