Cod sursa(job #2610103)

Utilizator Vlad_AnicaAnica-Popa Vlad-Ioan Vlad_Anica Data 4 mai 2020 14:33:30
Problema Sortare prin comparare Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
int v[500000],a[500000];
void mergesort(int v[], int st, int dr )
{
    if(st!=dr)
    {
        int mij=((st+dr)>>1);
        mergesort(v,st,mij);
        mergesort(v,mij+1,dr);
        int k,j,cpst=st;
        k=st;
        j=mij+1;
        while(k<=mij && j<=dr)
        {
            if(v[k]<v[j])
                a[st++]=v[k++];
            else
                a[st++]=v[j++];
            cout << a[st-1] << " ";
        }

        while(k<=mij)
        {
            a[st++]=v[k++];
            cout << a[st-1] << " ";
        }

        while(j<=dr)
        {
            a[st++]=v[j++];
            cout << a[st-1] << " ";
        }

        for(j=cpst;j<=dr;j++)
        {
            v[j]=a[j];

        }
        cout << '\n';

    }

}

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