Se citeste de la tastatura un tablou neordonat cu n elemente. Dorim sa-l ordonam crescator.
#include<iostream>
using namespace std;
int a[100],n;
void interclasare(int st, int m, int dr)
{int i,j,b[100],k;
i=st;
j=m+1;
k=0;
while(i<=m && j<=dr)
if(a[i]<=a[j])
b[k++]=a[i++];
else b[k++]=a[j++];
while(i<=m)
b[k++]=a[i++];
while(j<=dr)
b[k++]=a[j++];
for(i=st;i<=dr;i++)
a[i]=b[i-st];
}
void mergesort(int st, int dr)
{if(st<dr)
{int m;
m=(st+dr)/2;
mergesort(st,m);
mergesort(m+1,dr);
interclasare(st,m,dr);
}
}
int main()
{int i;
cout<<"n=";cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
mergesort(0,n-1);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}