def bucket_sort(li): # 【1,2】
"""桶排序"""
# 建立max(li)个桶
buckets = [0] * (max(li)+1) # 【0,0,0】
# 创建一个空列表new
new = []
# 遍历li 桶中以元素为下标的值 +1 【0,1,1】
for i in li:
buckets[i] += 1
for j in range(max(li)+1):
if buckets[j] > 0: # buckets[1] == 1 > 0
new.extend([j]*buckets[j]) # [1]*1
return new # [1,2]
1
bucket_sort([1,4,1,5,1,5,45,4673,3623,246,1,62,754,1,234,645,326])
>> [1, 1, 1, 1, 1, 4, 5, 5, 45, 62, 234, 246, 326, 645, 754, 3623, 4673]