//希望对你有帮助
template
MERGE(T* A, int p,int q, int r)
{
int N1 = q - p + 1, N2 = r - q, i, j;
T* L = new T[N1+2];//未用L[0]与R[0]
T* R = new T[N2+2];
for(i = 1;i < N1+1;i++)
L[i] = A[p + i - 1];
for(j = 1;j < N2 + 1;j++)
R[j] = A[q+j];
L[N1+1] = maxnum;//定义一个永远不可达到的最大值,作为哨兵
R[N2+1] = maxnum;
i = 1;j = 1;
for(int k = p;k <= r;k++)
if(L[i] <= R[j])
{
A[k] = L[i];
i++;
}
else
{
A[k] = R[j];
j++;
}
}
template
MERGE-SORT(T * A, int p, int r)
{
if(p < r)
q = (p + r) / 2;
MERGE-SORT(A,p,q);
MERGE-SORT(A,q+1,r);
MERGE(A,p,q,r);
}