-4

这是python中的合并排序算法

def MERGE_SORT(A, p, r):
    if p < r:
        q = int((p+r)/2)
    MERGE_SORT(A, p, q)
    MERGE_SORT(A, q+1, r)
    MERGE(A, p, q, r)

def MERGE(A, p, q, r):
    n1 = q-p+1
    n2 = r-q
    L = []
    R = []
    for i in range(0, n1, 1):
        L[i] = A[p+i]
    for j in range(0, n2, 1):
        R[j] = A[q+j+1]
    L[n1]=float("inf")
    R[n2]=float("inf")
    i = 0 
    j = 0
    for k in range(p, r+1, 1):
        if L[i] < R[j] and L[i]==R[j]:
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1
    return A
4

1 回答 1

2
def MERGE_SORT(A, p, r):
    if p < r:
        q = int((p+r)/2)
    MERGE_SORT(A, p, q)

If p < ris False,q不会被定义,所以你会在下一行得到一个未绑定的本地错误。您需要q在所有情况下进行定义。

于 2013-07-13T08:50:24.683 回答