Cod sursa(job #1314080)

Utilizator danielmaxim95FMI Maxim Daniel danielmaxim95 Data 11 ianuarie 2015 15:07:14
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
//MergeSort
#include <fstream>
#define NMAX 500003
using namespace std;

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

int v[NMAX],c[NMAX],n;

void intercl(int s, int p, int d)
{
    int i=s, j=p+1, k=1;

    if(s>d)
    {
        while ((i<=p)&&(j<=d))
            if(v[i]<v[j])
                c[k++]=v[i++];
            else
                c[k++]=v[j++];

        while(i<=p)
            c[k++]=v[i++];
        while (j<=d)
            c[k++]=v[j++];

        k=1;
        for (i=s;i<=d;i++)
            v[i]=c[k++];
    }
}

void msort(int low, int high)
{
    int pivot;
    if(low<high)
    {
        pivot=(low+high)/2;
        msort(low,pivot);
        msort(pivot+1,high);
        intercl(low,pivot,high);
    }
}

int main()
{
    f >> n;
    for(int i=1; i<=n; i++)
        f >> v[i];

    msort(1, n);

    for(int i=1; i<=n; i++)
        g << v[i] << " ";

    return 0;
}