Cod sursa(job #3242071)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 8 septembrie 2024 15:52:10
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("popcnt,avx2")
#define ll long long
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define all(a) (a).begin(),(a).end()
using namespace std;
const ll maxn = 1e5 + 5;


ll partition(vector<ll>& a, ll low, ll high) {
    ll pivot = a[low];
    ll i = low - 1;
    ll j = high + 1;
    while (1) {
        i++;
        while (a[i] < pivot) {
            i++;
        }
        j--;
        while (a[j] > pivot) {
            j--;
        }
        if (i >= j) {
            return j;
        }
        swap(a[i], a[j]);
    }
}

void quick_sort(vector<ll>& a, ll low, ll high) {
    if (low >= high) {
        return;
    }
    ll pivot = partition(a, low, high);
    quick_sort(a, pivot + 1, high);
    quick_sort(a, low, pivot); 
}

void solve() {
    FILE* input = fopen("algsort.in", "r");
    FILE* output = fopen("algsort.out", "w");
    ll n;
    fscanf(input, "%lld", &n);
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
        fscanf(input, "%lld", &a[i]);
    }
    quick_sort(a, 0, n - 1);
    for (ll i = 0; i < n; i++) {
        fprintf(output, "%lld", a[i]);
    }
}
 
int main() {
    ll t = 1;
    // fin >> t;
    while (t--) {
        solve();
    }
    return 0;
}