Cod sursa(job #1624040)

Utilizator tudormaximTudor Maxim tudormaxim Data 1 martie 2016 23:44:19
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

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

const int nmax = 500005;
int v[nmax];

void quicksort(int st, int dr)
{
    int i=st, j=dr, pivot=(st+dr)>>1;
    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) quicksort(st, j);
    if(i < dr) quicksort(i, dr);
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, i;
    fin >> n;
    for(i=1; i<=n; i++)
        fin >> v[i];
    quicksort(1, n);
    for(i=1; i<=n; i++)
        fout << v[i] << " ";
    fin.close();
    fout.close();
    return 0;
}