Cod sursa(job #2435206)

Utilizator PaulTPaul Tirlisan PaulT Data 3 iulie 2019 14:16:10
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
using namespace std;

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

int a[500005];

void QuickSort(int st, int dr);

int main()
{
	int n;
	fin >> n;
	for (int i = 1; i <= n; i++)
		fin >> a[i];
	QuickSort(1, n);
	for (int i = 1; i <= n; i++)
		fout << a[i] << ' ';
	
	fin.close();
	fout.close();
}

void QuickSort(int st, int dr)
{
	int i = st, j = dr;
	int pivot = a[(st + dr) / 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 (st < j) QuickSort(st, j);
	if (i < dr) QuickSort(i, dr);
}