排序算法 互动版

归并排序算法伪代码

//归并排序
MERGE_SORT(A,p,q)
{
    if p < q
       m = (p + q) / 2
       MERGE_SORT(A,p,m)
       MERGE_SORT(A,m+1,q)
       MERGE(A,p,m,q)
}

MERGE(A,p,m,q)
{
    a = m - p + 1
    b = q - m
    create array L[a+1] & R[b+1]
    for i=1 to a
        L[i] = A[p+i-1]
    for i=1 to b
        R[i] = A[m+i]
    L[a+1] = INFINITY
    R[b+1] = INFINITY
    i = j = 1
    for k=p to q
        if L[i] <= R[j]
           A[k] = L[i]
           i++
        else
           A[k] = R[j]
           j++
}
用归并排序算法对数组arr[10] = {8, 5, 10, 12, 7, 6, 15, 9, 11, 3}从小到大排序。