#include<iostream>
using namespace std;
void Merge(int* A, int p, int q, int r)
{
int i,j,k;
int n1 = q - p + 1;
int n2 = r - q;
int* L = new int[n1+2];
int* R = new int[n1+2];
//将长度为r的数组元素分别储存在长度为n1、n2的数组中
for(i=1;i<=n1;i++)
{
L[i] = A[p+i-1];
}
for(j=1;j<=n2;j++)
{
R[j] = A[q+j];
}
L[n1+1] = 0x7FFFFFFF;//预防L[n1+1]<R[j],出错;相当于赋权值为无限大
R[n2+1] = 0x7FFFFFFF;//预防R[n2+1]<L[i],出错;相当于赋权值为无限大
i = 1;
j = 1;
for(k=p;k<=r;k++)
{
if(L[i] <= R[j])
{
A[k] = L[i];
i++;
}
else
{
A[k] = R[j];
j++;
}
}
}
void MergeSort(int* A, int p, int r)
{
if(p < r)
{
int q = (p+r)/2;
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A,p,q,r);
}
}
int main()
{
int a[10] = {214,3636,22,55,37,45,12,54,264,52};
MergeSort(a,0,9);
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}