1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| def count_sort(a, start, end): cnts = [0 for _ in range(start, end+1)] for e in a: cnts[e-start]+=1 print(cnts) sum = 0 for i in range(1, len(a)): cnts[i] += cnts[i-1] new_a = a[:] for e in a: print(cnts, e-start) new_a[cnts[e-start]-1] = e cnts[e-start]-=1 return new_a if __name__ == "__main__": import random a = [random.randrange(10) for _ in range(10)] print(a) print(count_sort(a, 0, 10))
|