Cod sursa(job #2822828)

Utilizator AswVwsACamburu Luca AswVwsA Data 25 decembrie 2021 19:02:55
Problema Sortare prin comparare Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
#include <fstream>
#include <algorithm>
#include <vector>
#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])
        	{
        		if (x1 == pivot)
        			pivot = x2;
        		else if (x2 == pivot)
        			pivot = x1;
        		swap(v[x1], v[x2]);
        		x1++;
        		x2--;
        	}
        	else 
        		x2--;
        }
        else 
        {
        	if (v[x2] >= v[pivot])
        		{
        			x2--;
        			x1++;
        		}
        	else
        		x1++;
        }
    }
    /*cout << "Pivot: " << pivot << "\n"; 
    cout << "Prima partitie: ";
    for (int i = st; i <= pivot - 1; i++)
        cout << v[i] << " ";
    cout << "\nA doua partitie: ";
    for (int i = pivot + 1; i <= dr; i++)
        cout << v[i] << " ";
    cout << "\n\n\n";*/
    quicksort(st, pivot - 1);
    quicksort(pivot + 1, dr);
}
int main()
{
    int i, n;
    cin >> n;
    for (i = 1; i <= n; i++)
        cin >> v[i];
    /*int pivot = (1 + n) / 2, st = 1, dr = n;
    while (st <= dr)
    {
        if (v[st] >= v[pivot])
        {
            if (v[dr] > v[pivot])
                dr--;
            else 
            {
                if (st == pivot)
                    pivot = dr;
                swap(v[st], v[dr]);
                st++;
            }
        }
        else 
            {
                if (v[dr] < v[pivot])
                    st++;
                else 
                {
                    dr--;
                    st++;
                }
            }
    }*/
    quicksort(1, n);
    for (i = 1; i <= n; i++)
        cout << v[i] << " ";
    //cout << "\n" << pivot;
}