Cod sursa(job #2840654)

Utilizator cdenisCovei Denis cdenis Data 28 ianuarie 2022 16:22:55
Problema Sortare prin comparare Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int MAX=5e5+5;
int v[MAX],n;

void QuickSort_pbinfo(int v[], int st, int dr)
{
	if(st < dr)
	{
		//pivotul este inițial v[st]
		int m = (st + dr) / 2;
		int aux = v[st];
		v[st] = v[m];
		v[m] = aux;
		int i = st , j = dr, d = 0;
		while(i < j)
		{
			if(v[i] > v[j])
			{
				aux = v[i];
				v[i] = v[j];
				v[j] = aux;
				d = 1 - d;
			}
			i += d;
			j -= 1 - d;
		}
		QuickSort_pbinfo(v, st , i - 1);
		QuickSort_pbinfo(v, i + 1 , dr);
	}
}

int main()
{
    fin >> n;
    for(int i=1;i<=n;i++)
        fin >> v[i];
    QuickSort_pbinfo(v,1,n);
    for(int i=1;i<=n;i++)
        fout << v[i] << " ";
    return 0;
}