Cod sursa(job #2739237)

Utilizator FraNNNkieFrancesco-Gregorio Petrovici FraNNNkie Data 7 aprilie 2021 12:39:42
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <cstdlib>
#define NMAX 500005
using namespace std;
int v[NMAX], n, i;
int get_partition(int v[], int left, int right, int pivot)
{
    while(left <= right)
    {
        while(v[left] < pivot)
            left ++;
        while(v[right] > pivot)
            right --;
        if(left <= right)
        {
            swap(v[left], v[right]);
            left ++;
            right --;
        }
    }
    return left;
}
void QSORT(int v[], int left, int right)
{
    if(left < right)
    {
        int random = left + rand() % (right - left);
        int pivot = v[random];
        int index = get_partition(v, left, right, pivot);
        QSORT(v, left, index - 1);
        QSORT(v, index, right);
    }
    else
        return;
}
int main()
{
    ifstream f("algsort.in");
    ofstream g("algsort.out");

    f >> n;
    for(i = 0; i < n; ++i)
        f >> v[i];
    QSORT(v, 0, n - 1);
    for(i = 0; i < n; ++i)
        g << v[i] << " ";

    f.close();
    g.close();
    return 0;
}