Cod sursa(job #2450115)

Utilizator ejoi2019Ejoi 2019 ejoi2019 Data 21 august 2019 23:09:10
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int N=500007;
int BLACK;
int n;
int a[N];
int nw[N];
int len;


void NIGGA_SORT(int l,int r)
{
        if(r-l+1<=BLACK)
        {
                sort(a+l,a+r+1);
                return;
        }
        int m=(l+r)/2;
        NIGGA_SORT(l,m);
        NIGGA_SORT(m+1,r);
        int pa=l,pb=m+1;
        for(int j=l;j<=r;j++)
        {
                int tp=0;
                if(pa>m)
                        tp=2;
                if(pb>r)
                        tp=1;
                if(tp==0)
                        if(a[pa]<a[pb])
                                tp=1;
                        else
                                tp=2;
                if(tp==1)
                        nw[j]=a[pa++];
                else
                        nw[j]=a[pb++];
        }
        for(int j=l;j<=r;j++)
                a[j]=nw[j];
}

int main()
{
        freopen("algsort.in","r",stdin),
        freopen("algsort.out","w",stdout);

        scanf("%d",&n);
        BLACK=1;
        for(int i=1;i<=n;i++)
                scanf("%d",&a[i]);
        NIGGA_SORT(1,n);
        for(int i=1;i<=n;i++)
                printf("%d ",a[i]);
        printf("\n");
        return 0;
}