#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_SIZE = 100;
int b[MAX_SIZE];// Type *b = new Type[right-left+1];
template<class Type>
void Merge(Type c[], Type d[], int left, int mid, int right)
{//c[] l->m ; d[] m+1->r
int i=left,j=mid+1;
int k=left;
while ( i<=mid && j<=right )
{
if ( c[i] <= c[j] )
d[k++] = c[i++];
else
d[k++] = c[j++];
}
if ( i>mid )
{
for ( int q=j; q<=right; q++ )
d[k++] = c[q];
}
else if( j>right )
{
for ( int q=i; q<=mid; q++ )
d[k++] = c[q];
}
}
template<class Type>
void Copy(Type c[], Type d[], int left, int right)
{
for ( int q=left; q<=right; q++ )
c[q] = d[q];
}
template<class Type>
void MergeSort(Type a[], int left, int right)
{
if (left<right)
{
int i = (left+right)/2;
MergeSort(a,left,i);
MergeSort(a,i+1,right);
Merge(a,b,left,i,right);
Copy(a,b,left,right);
}
}
int main()
{
int n = 5;
int x[] = {5,1,7,4,2};
MergeSort(x,0,n-1);
for ( int i=0; i<=n-1; i++ )
cout << x[i] << " ";
cout << endl;
return 0;
}