Cod sursa(job #3254624)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 8 noiembrie 2024 11:59:22
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
using namespace std;
ifstream cin("algsort.in");
ofstream cout("algsort.out");
int n,v[500001],a[500001];
void merge_sort(int st,int dr){
    if(st>=dr)
        return ;
    int mid=(st+dr)/2;
    merge_sort(st,mid);
    merge_sort(mid+1,dr);
    int i=st,j=mid+1,k=st;
    while(i<=mid&&j<=dr)
        if(v[i]<=v[j])
            a[k++]=v[i++];
        else
            a[k++]=v[j++];
    while(i<=mid)
        a[k++]=v[i++];
    while(j<=dr)
        a[k++]=v[j++];
    for(int i=st;i<=dr;i++)
        v[i]=a[i];
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>v[i];
    merge_sort(1,n);
    for(int i=1;i<=n;i++)
        cout<<v[i]<<" ";
    return 0;
}