--- 基数排序 --- 效率和数据分布有关 如果数字有特别大的 (k),但是 range 比较小 (n),不太适合 li = [randint(0, k) for _ in range(n)] 多关键字排序
def radix_sort(li):
# 最大值是几位数,就做几次排序
max_num = max(li)
# it = floor(log10(max_num))
it = 0
while 10 ** it <= max_num:
buckets = [[] for _ in range(10)]
# 分桶
for val in li:
digit = (val // 10 ** it) % 10
buckets[digit].append(val)
# 把数重新写回 li
li.clear()
for buc in buckets:
li.extend(buc)
it += 1