Cod sursa(job #2170837)

Utilizator LittleWhoFeraru Mihail LittleWho Data 15 martie 2018 09:57:12
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = static_cast<int> (5e5 + 1);
int v[nmax];
int n;

void quick(int left, int right) {
    int i = left;
    int j = right;
    int pivot = v[(left + right) / 2];

    while (i <= j) {
        while (v[i] < pivot) i++;
        while (v[j] > pivot) j--;
        if (i <= j) {
            swap(v[i], v[j]);
            i++;
            j--;
        }
    }

    if (j > left) {
        quick(left, j);
    }
    if (i < right) {
        quick(i, right);
    }
}

int main() {
    freopen("carici.in", "r", stdin);

    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &v[i]);
    }

    quick(1, n);

    for (int i = 1; i <= n; i++) {
        printf("%d ", v[i]);
    }
    cout << "\n";

    return 0;
}