Cod sursa(job #2249660)

Utilizator MaxTeoTeo Oprescu MaxTeo Data 30 septembrie 2018 09:54:27
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("algsort.in"); ofstream g("algsort.out");

int n;
int v[500005];

void Read()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> v[i];
}

int Random(int left, int right)
{
    srand(time(NULL));
    return left + rand() % (right - left);
}

void QuickSort(int left, int right)
{
    int low = left, high = right;
    //int pivot = Random(left, right);
    int pivot = (left + right) / 2;
    do
    {
        while(low < right && v[low] < pivot)
            ++low;

        while(high > left && v[high] > pivot)
            --high;

        if(low <= high)
        {
            swap(v[low], v[high]);
            ++low;
            --high;
        }

    }while(low <= high);

    if(left < high)
        QuickSort(left, high);
    if(low < right)
        QuickSort(low, right);
}

void Print()
{
    for(int i = 1; i <= n; ++i)
        g << v[i] << " ";
    g << "\n";
}

int main()
{
    Read();
    QuickSort(1, n);
    Print();
    return 0;
}