Cod sursa(job #2285911)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 19 noiembrie 2018 15:03:43
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;

int v[2][500010];

void msort(int st, int dr, int c)
{
    if (st == dr) {
        v[c][st] = v[0][st];
        return;
    }
    int mij = (st + dr) / 2;
    msort(st, mij, 1 - c);
    msort(mij + 1, dr, 1 - c);
    for (int i = st, j = mij + 1, poz = st; poz <= dr; poz++) {
        if (i <= mij && (j > dr || v[1 - c][i] < v[1 - c][j]))
            v[c][poz] = v[1 - c][i++];
        else
            v[c][poz] = v[1 - c][j++];
    }
}

int main()
{
    FILE *in = fopen("algsort.in", "r");
    FILE *out = fopen("algsort.out", "w");

    int n;
    fscanf(in, "%d", &n);

    for (int i = 0; i < n; i++)
        fscanf(in, "%d", v[0] + i);

    msort(0, n - 1, 0);

    for (int i = 0; i < n; i++)
        fprintf(out, "%d ", v[0][i]);
    fprintf(out, "\n");

    return 0;
}