Cod sursa(job #2890101)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 14 aprilie 2022 16:10:51
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 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 i = st;
    int j = dr;
    while(i <= j)
    {
        while(v[i] < pivot)
            i++;
        while(v[j] > pivot)
            j--;
        if(i <= j)
        {
            swap(v[i], v[j]);
            i++;
            j--;
        }
    }
    quicksort(st, j);
    quicksort(i, 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;
}