#include "stdafx.h"
#include <iostream>
using namespace std;
void Merge(int* a,int p,int q,int r)
{
int u = q-p+1;
int v = r-q;
int *L = new int[u+1];
int *R = new int [v+1];
for(int i=0;i<q-p+1;i++)
{
L[i] = a[i+p];
}
for(int j=0;j<r-q;j++)
{
R[j] = a[j+q+1];
}
L[u] = 1000;
R[v] = 1000;
int i=0,j=0;
for(int k=p;k<r+1;k++)
{
if(L[i]<=R[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = R[j];
j++;
}
}
delete[] L;
delete[] R;
}
void Mergesort(int* a,int n1,int n2)
{
if(n1<n2)
{
int n3 = (n1+n2)/2;
Mergesort(a,n1,n3);
Mergesort(a,n3+1,n2);
Merge(a,n1,n3,n2);
}
}
int main()
{
int a[11] = {15,36,5,16,8,10,21,26,108,0,2};
Mergesort(a,0,10);
for(int i=0;i<11;i++)
cout<<a[i]<<endl;
}
<img src="https://img-blog.csdn.net/20141028122257334?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvZHIxOTg5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />