在python中实现计数排序

时间:2018-09-03 22:29:48

标签: python counting-sort

我试图从《 Cormen Book》中实现计数排序,但是python在更改假定为输出数组的B的值时,对原始列表做了一些奇怪的事情

def counting_sort(A):

    A[0:0]=[0]
    C=[]
    B=A
    for i in range(max(A)+1):
        C.append(0);
    for j in range(1,len(A)):
        C[A[j]]=C[A[j]]+1
    for k in range(1,len(C)):
        C[k]=C[k]+C[k-1]
    print(C)
    for l in range(len(A)-1,0,-1):
        B[C[A[l]]]=A[l]
        C[A[l]]= C[A[l]]-1
        print(a)
        print(C)
    B.pop(0)
    return B
A=[3,5,6,0,2,9,1,8,2]

print(counting_sort(A))

0 个答案:

没有答案