Cod sursa(job #2737040)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 4 aprilie 2021 12:47:25
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
const int maxn = 500005;
int v[maxn];

void quicksort(int st, int dr)
{
    if(st >= dr)
        return;
    int pivot = v[(st + dr) / 2];
    int x = st;
    int y = dr;
    while(x <= y)
    {
        while(v[x] < pivot)
            x++;
        while(v[y] > pivot)
            y--;
        if(x <= y)
            swap(v[x++], v[y--]);
    }
    if(y > st)
        quicksort(st, y);
    if(x < dr)
        quicksort(x, dr);
}

int main()
{
    int n;
    in >> n;
    for(int i = 1; i <= n; i++)
        in >> v[i];
    quicksort(1, n);
    for(int i = 1; i <= n; i++)
        out << v[i] << " ";
    out << "\n";
    return 0;
}