Cod sursa(job #1723543)

Utilizator BlackNestaAndrei Manaila BlackNesta Data 30 iunie 2016 22:01:51
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, a[500050];

void QS(int st,int dr)
{
    int piv;
    int i = st;
    int j = dr;
    piv = a[(i + j) / 2];
    while(i <= j)
    {
        while(i < dr && a[i] < piv) i++;
        while(j > st && a[j] > piv) j--;
        if(i <= j)
        {
            swap(a[i], a[j]);
            i++;
            j--;
        }
    }
    if (st < j) QS(st, j);
    if (i < dr) QS(i, dr);
}

int main()
{
    f >> n;
    for(int i = 1; i <= n; i++)
        f >> a[i];
    f.close();
    QS(1, n);
    for(int i = 1; i <= n; i++)
        g << a[i] << " ";
    g << "\n";
    g.close();
    return 0;
}