Cod sursa(job #3252614)

Utilizator ninja_legend_11Vlad Marin-Perianu ninja_legend_11 Data 30 octombrie 2024 11:51:20
Problema Sortare prin comparare Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define MAX 500000

using namespace std;

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

int32_t n, i, j, x, a[MAX], random1;

void Sort(int32_t st, int32_t dr)
{
    if(st < dr)
    {
        srand(time(NULL));
        random1 = st+rand()%(dr-st);
        swap(a[random1], a[dr]);
        i = st; j = dr; x = 0;
        while(i < j)
        {
            if(a[i] > a[j])
            {
                swap(a[i], a[j]);
                x = 1-x;
            }
            i += x;
            j -= 1-x;
        }
        Sort(st, i-1);
        Sort(i+1, dr);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);
    fin >> n;
    for(i=0; i < n; i++) fin >> a[i];
    Sort(0, n-1);
    for(i=0; i < n; i++) fout << a[i] << ' ';
}