Cod sursa(job #959942)

Utilizator manciu_ionIon Manciu manciu_ion Data 9 iunie 2013 14:17:11
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 = 500000;
int N, a[NMax];

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

void swap(int* st, int* dr);
void Qsort(int st, int dr);

int main()
{
    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';

    return 0;
}

void swap(int* st, int* dr)
{
    int aux=*st;
    *st = *dr;
    *dr = aux;
}

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);
}