C++代码实现:
MERGE(A,p,q,r)
n1 <- q-p+1
n2 <- r-q;
create arrays L[1...n1] and R[1...n2]
for i <- 1 to n1
do L[i] <- A[p+i-1]
for j <- 1 to n2
do R[j] <- A[q+j]
m1 <- n1
m2 <- n2
i <- 1
j <- 1
for k <- p to r
do if m1 equal 0
then for j <- j to n2
do if k < r
then A[k] <- R[j]
j <- j+1
k <- k+1
continue
if m2 equal 0
then for i <- i to n1
do if k < r
then A[k] <- L[i]
i <- i+1
k <- k+1
continue
if L[i] <= R[j]
then A[k] <- L[i]
i <- i+1
m1 <- m1-1
else A[k] <- R[j]
j <- j+1
m2 <- m2-1