Cod sursa(job #959948)

Utilizator manciu_ionIon Manciu manciu_ion Data 9 iunie 2013 14:23:46
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>

using namespace std;

const int NMax = 500000;
int N, a[NMax];

void Qsort(int st, int dr);

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

    int i;
    f >> N;
    for (i = 0; i < N; ++i)
       f >> a[i];

    Qsort(0,N-1);

    for (i = 0; i < N; ++i)
        g << a[i] << ' ';
    g << '\n';

    f.close();
    g.close();

    return 0;
}

void Qsort(int st, int dr)
{
    int i = st, j = dr, aux;
    int x = a[(i+j) >> 1];
    do {
        while (a[i] < x) ++i;
        while (a[j] > x) --j;
        if (i <= j)
        {
            aux = a[i];
            a[i] = a[j];
            a[j] = aux;
            ++i; --j;
        }
    } while (i < j);

    if (j > st) Qsort(st,j);
    if (i < dr) Qsort(i,dr);
}