Cod sursa(job #2766214)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 31 iulie 2021 13:10:16
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int len, v[500005], w[500005];

void combine(int a, int b, int c){
    int i=a;
    int n=b;
    int j=b+1;
    int m=c;
    w[0]=0;

    while(i <= n && j <= m)
        if(v[i] <= v[j]){
            w[++w[0]]=v[i];
            i++;
        }else{
            w[++w[0]]=v[j];
            j++;
        }

    for(int pas=i; pas <= n; pas++)
        w[++w[0]] = v[pas];
    for(int pas=j; pas <= m; pas++)
        w[++w[0]] = v[pas];

    for(int pas=a; pas <= c; pas++)
        v[pas] = w[pas-a+1];
}

void dei(int st, int dr){
    if(st < dr){
        int md=(st+dr)/2;
        dei(st, md);
        dei(md+1, dr);
        combine(st, md, dr);
    }
}

int main (){
    fin>>len;
    for(int i=1; i<=len; i++)
        fin>>v[i];
    dei(1, len);
    for(int i=1; i<=len; i++)
        fout<<v[i]<<" ";
    return 0;
}