Cod sursa(job #3326406)

Utilizator horatiu.avramAvram Popa Cristian Horatiu horatiu.avram Data 28 noiembrie 2025 18:40:33
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

const int MAXN=5e5;

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

int v[MAXN+1];

void sort(int begin,int end) {
    int b=begin,e=end;
    int pivot=v[(b+e)>>1],aux;
    while(v[b]<pivot) {
        b++;
    }
    while(v[e]>pivot) {
        e--;
    }
    while(b<e) {
        aux=v[b];
        v[b]=v[e];
        v[e]=aux;
        b++;
        e--;
        while(v[b]<pivot) {
            b++;
        }
        while(v[e]>pivot) {
            e--;
        }
    }
    if(begin<e) {
        sort(begin,e);
    }
    if(e+1<end) {
        sort(e+1,end);
    }
}

int main() {
    int n,i;
    fin>>n;
    for(i=0; i<n; i++) {
        fin>>v[i];
    }
    sort(0,n-1);
    for(i=0; i<n; i++) {
        fout<<v[i]<<' ';
    }
    return 0;
}