Cod sursa(job #1707547)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 25 mai 2016 14:28:26
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <cstdio>
#include <algorithm>
#include <vector>

#define pb push_back

using namespace std;

int v[500010];
vector <int> sol;

inline vector <int> ms (int x, int y)
{
    vector <int> a, b;

    if (x == y)
    {
        a.pb (v[x]);
        return a;
    }

    int mij = (x + y) >> 1;
    a = ms (x, mij);
    b = ms (mij + 1, y);

    vector <int> rez;

    int i = 0, j = 0;
    while (i < a.size () || j < b.size ())
    {
        if (i < a.size () && (j == b.size () || a[i] < b[j]))
            rez.pb (a[i++]);

        else rez.pb (b[j++]);
    }

    return rez;
}

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

    int n;
    scanf ("%d", &n);

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

    sol = ms (1, n);

    for (auto &it : sol)
        printf ("%d ", it);

    printf ("\n");

    return 0;
}