Cod sursa(job #1985573)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 28 mai 2017 08:50:19
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>

using namespace std;

const int nMax = 500005;

int n;
int v[nMax];

void citire()
{
    ifstream in("algsort.in");
    in >> n;
    for(int i = 1; i <= n; ++i)
        in >> v[i];
    in.close();
}

void quick_sort(int st, int dr)
{
    int pivot = (st + dr) / 2;
    int i = st, j = dr;
    while(i <= j)
    {
        while(v[i] < v[pivot])
            ++i;
        while(v[j] > v[pivot])
            --j;
        if(i <= j)
        {
            swap(v[i], v[j]);
            ++i;
            --j;
        }
    }
    if(st< j)
        quick_sort(st, j);
    if(i < dr)
        quick_sort(i, dr);
}

void afisare()
{
    ofstream out("algsort.out");
    for(int i = 1; i <= n; ++i)
        out << v[i] << " ";
    out.close();
}

int main()
{
    citire();
    quick_sort(1, n);
    afisare();
    return 0;
}