【数组】--Cantor数组

def Cantor(arr):
    cantorarr = [0]*len(arr)
    for i in range(len(arr)):
        for j in range(i+1, len(arr)):
            if arr[j] < arr[i]:
                cantorarr[i] += 1
    return cantorarr            

def CantorRebuild(cantor):
    N = len(cantor)
    A = [0]*N
    B = range(1, N+1)
    for i in range(N):
        A[i] = B[cantor[i]]
        B.pop(cantor[i])
    return A

def CantorRebuild2(cantor):
    N = len(cantor)
    A = [0]*N

    for i in range(N):
        for j in range(N):
            if cantor[j] == 0:
                A[j] = i + 1
                cantor[j] -= 1
                break
            else:
                cantor[j] -= 1

    return A

if __name__ == "__main__":
    arr = [4, 6, 1, 5, 3, 2]
    print arr
    print Cantor(arr)
    print CantorRebuild2(Cantor(arr))

你可能感兴趣的:(【数组】--Cantor数组)