基数排序

/ Python / 0 条评论 / 277浏览

def radix_sort(array): max_num = max(array) place = 1 while max_num >= 10place: place += 1 for i in range(place): buckets = [[] for _ in range(10)] for num in array: radix = int(num/(10i) % 10) buckets[radix].append(num) j = 0 for k in range(10): for num in buckets[k]: array[j] = num j += 1 return array

if name == 'main': array = [25, 17, 33, 17, 22, 13, 32, 15, 9, 25, 27, 18] print(radix_sort(array))