书p108,这是一个时间复杂度为n的算法,输入需要确定值的变化范围,空间复杂度为n
def CountingSort(A,k):
B = [0]*len(A)
C = [0]*k
for i in range(len(A)):
C[A[i]] += 1
for i in range(1,len(C)):
C[i] += C[i-1]
for i in range(len(A)):
B[C[A[i]]-1] = A[i]
C[A[i]] -= 1
return B
A = [2,5,3,0,2,3,0,3]
k=5+1
print(CountingSort(A,k))