Cod sursa(job #1242187)

Utilizator radudorosRadu Doros radudoros Data 14 octombrie 2014 01:08:49
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <time.h>
using namespace std;

int a[500001];

int partition(int i , int s)
{
	int l = i - 1;
	for (int j = i; j <= s;j++)
	if (a[j] <= a[s])
	{
		l++;
		int aux = a[j];
		a[j] = a[l];
		a[l] = aux;
	}
	return l;
}

void quicksort(int l, int r)
{
	if (r - l < 1)
		return;
	if (r - l == 1)
	{
		if (a[l]>a[r])
		{
			int aux=a[l];
			a[l] = a[r];
			a[r] = aux;
		}
		return;
	}

	int k = a[rand() % (r - l + 1) + l];
	int i = l;
	int j = r;
	while (i <= j)
	{
		while (a[i] < k)i++;
		while (a[j] > k)j--;
		if (i <= j)
		{
			int aux = a[i];
			a[i] = a[j];
			a[j] = aux;
			i++;
			j--;
		}
	}



	quicksort(l,i-1);
	quicksort(i,r);
		
	
}


int main()
{
	ifstream fin("algsort.in");
	ofstream fout("algsort.out");
	int n;
	fin >> n;
	for (int i = 1; i <= n; i++)
	{
		fin >> a[i];
	}
	srand(6782);
	quicksort(1, n);
	for (int i = 1; i <= n; i++)
	{
		fout << a[i] << ' ';
	}
}