Cod sursa(job #1953360)

Utilizator tudormaximTudor Maxim tudormaxim Data 4 aprilie 2017 19:34:35
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
using namespace std;

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

const int maxn = 5e5 + 5;
int V[maxn];

void QuickSort (int left, int right) {
    int i = left;
    int j = right;
    int pivot = V[(left + right) >> 1];
    while (i <= j) {
        while (V[i] < pivot) i++;
        while (V[j] > pivot) j--;
        if (i <= j) {
            swap(V[i], V[j]);
            i++;
            j--;
        }
    }
    if (left < j) QuickSort(left, j);
    if (i < right) QuickSort(i, right);
}


int main () {
    ios_base :: sync_with_stdio (false);
    int n, i;
    fin >> n;
    for (i = 1; i <= n; i++) {
        fin >> V[i];
    }
    QuickSort(1, n);
    for (i = 1; i <= n; i++) {
        fout << V[i] << " ";
    }
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}