Cod sursa(job #1224183)

Utilizator Impaler_009Mihai Nitu Impaler_009 Data 30 august 2014 00:30:57
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

ifstream fin ("algsort.in");
ofstream fout ("algsort.out");

int n,a[500010];

void quicksort (int p, int q)
{
    if (p >= q)
    {
        return;
    }

    int i = p-1, j = q+1, x = a[p+rand()%(q-p+1)];

    while (i < j)
    {
        do ++i; while (a[i] < x);
        do --j; while (a[j] > x);

        if (i < j)
          swap (a[i],a[j]);
    }

    quicksort (p,j);
    quicksort (j+1,q);
}

int main()
{
    fin>>n;

    srand (time(NULL));

    for (int i=1; i<=n; ++i)
      fin>>a[i];

    quicksort (1,n);

    for (int i=1; i<=n; ++i)
      fout<<a[i]<<" ";
}