Cod sursa(job #1692573)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 21 aprilie 2016 11:08:30
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

#define NMax 500005
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");

int n;
int a[NMax];

void quicksort(int st,int dr){
    int pivot = (st + dr) / 2;
    int i = st;
    int j = dr;
    while(i <= j){
        while(a[pivot] > a[i] && i < dr)
            ++i;
        while(a[pivot] < a[j] && j > st)
            --j;
        if(i <= j){
            swap(a[i],a[j]);
            ++i;
            --j;
        }
    }
    if(j > st)
        quicksort(st,j);
    if(i < dr)
        quicksort(i,dr);
}
int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> a[i];
    quicksort(1,n);
    for(int i = 1; i <= n; ++i)
        g << a[i] <<' ';
    return 0;
}