Cod sursa(job #2556733)

Utilizator eduardpetcuPetcu Eduard Gabriel eduardpetcu Data 25 februarie 2020 10:16:47
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int a[500000],b[500000],n;
void interclasare(int st,int mj,int dr)
{int i,j,k;
for(int i=st; i<=dr; i++)
        b[i]=a[i];
    i=st;
    j=mj+1;
    k=st-1;
    while(i<=mj&&j<=dr)
        if(b[i]<=b[j])a[++k]=b[i++];
        else a[++k]=b[j++];

    for(int t=i; t<=mj; t++)
        a[++k]=b[t];
    for(int t=j; t<=dr; t++)
        a[++k]=b[t];
}
void divide(int st,int dr)
{
    int mj;
    while(st!=dr)
    {
        mj=(st+dr)/2;
        divide(st,mj);
        divide(mj+1,dr);
        interclasare(st,mj,dr);
    }
}
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
        f>>a[i];
    divide(1,n);
    for(int i=1; i<=n; i++)
        g<<a[i]<<" ";
    return 0;
}