Cod sursa(job #1692585)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 21 aprilie 2016 11:19:20
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 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 i,j,x;
    i = st;
    j = dr;
    x = a[(i + j) / 2];
    do{
        while(a[i] < x && i < dr)
            ++i;
        while(a[j] > x && j > st)
            --j;
        if(i <= j){
            swap(a[i],a[j]);
            ++i;
            --j;
        }
    }while(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;
}