Cod sursa(job #2058138)

Utilizator MaligMamaliga cu smantana Malig Data 5 noiembrie 2017 10:44:16
Problema Sortare prin comparare Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

#if 0
#define pv(x) cout<<#x<<" = "<<x<<"; "
#define pn cout<<"\n"
#else
#define pv(x)
#define pn
#endif

using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");

using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
#define pb push_back
#define mp make_pair
const int NMax = 5e5 + 5;
const ll inf = 1e18 + 5;
const int mod = 100003;
using zint = int;

int N;
int v[NMax];

void solve(int,int);
int part(int,int);

int main() {
    in>>N;
    for (int i=1;i <= N;++i) {
        in>>v[i];
    }

    solve(1,N);

    for (int i=1;i <= N;++i) {
        out<<v[i]<<' ';
    }

    in.close();out.close();
    return 0;
}

void solve(int st,int dr) {
    if (st >= dr) {
        return;
    }

    int idx = part(st,dr);
    solve(st,idx-1);
    solve(idx+1,dr);
}

int part(int st,int dr) {
    swap(v[dr],v[rand()%(dr-st+1) + st]);

    int pivot = v[dr];
    int i = st-1;
    for (int j=st;j <= dr;++j) {
        if (v[j] <= pivot) {
            swap(v[++i],v[j]);
        }
    }

    return i;
}