Cod sursa(job #2622485)

Utilizator george_sdGeorge george_sd Data 1 iunie 2020 13:15:49
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("algsort.in");
ofstream g("algsort.out");

void mergesort(vector<long long> &v, long long a=-1, long long b=-1){
    if(a<0 || b<0)
    {
        a=0;
        b=v.size();
    }
    if(a<b)
    {
        long long m=(a+b)/2;
        mergesort(v, a, m);
        mergesort(v, m+1, b);
        long long p=a, p1=0, p2=0, v1[m-a+1], v2[b-m];
        for(long long i=a; i<=m; i++)
            v1[i-a]=v[i];
        for(long long i=m+1; i<=b; i++)
            v2[i-m-1]=v[i];
        while(p1<m-a+1 && p2<b-m)
        {
            if(v1[p1]<=v2[p2])
                v[p++]=v1[p1++];
            else
                v[p++]=v2[p2++];
        }
        while(p1<m-a+1)
            v[p++]=v1[p1++];
        while(p2<b-m)
            v[p++]=v2[p2++];
    }
}

int main()
{
    long long n, aux;
    vector<long long> v;
    f>>n;
    for(int i=0; i<n; i++)
    {
        f>>aux;
        v.push_back(aux);
    }
    mergesort(v);
    for(int i=0; i<n; i++)
        g<<v[i]<<" ";
    return 0;
}