Cod sursa(job #2822816)

Utilizator AswVwsACamburu Luca AswVwsA Data 25 decembrie 2021 17:39:49
Problema Sortare prin comparare Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.01 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])
                x2--;
            else 
            {
                if (x1 == pivot)
                    pivot = x2;
                else if (x2 == pivot)
                    pivot = x1;
                swap(v[x1], v[x2]);
                x1++;
                x2--;
            }
        }
        else 
        {
            if (v[x2] < v[pivot])
                x1++;
            else 
            {
                x2--;
                x1++;
            }
        }
    } 
    /*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;
}