Cod sursa(job #2822810)

Utilizator AswVwsACamburu Luca AswVwsA Data 25 decembrie 2021 17:18:13
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#define ll long long
using namespace std;
const int NMAX = 500003;
ll v[NMAX];

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

void quicksort(int st, int dr)
{
	if (st >= dr)
		return ;
	int pivot = (st + dr) / 2, x1 = st, x2 = dr;
	while (x1 <= x2)
	{
		if (v[x1] >= v[pivot])
		{
			if (v[x2] > v[pivot])
				x2--;
			else 
			{
				if (x1 == pivot)
					pivot = x2;
				else if (x2 == pivot)
					pivot = x1;
				swap(v[x1], v[x2]);
				x1++;
			}
		}
		else 
		{
			if (v[x2] < v[pivot])
				x1++;
			else 
			{
				x2--;
				x1++;
			}
		}
	} 
	quicksort(st, pivot - 1);
	quicksort(pivot + 1, dr);
}
int main()
{
	int i, n;
	cin >> n;
	for (i = 1; i <= n; i++)
		cin >> v[i];
	quicksort(1, n);
	for (i = 1; i <= n; i++)
		cout << v[i] << " ";
}