首页 > 编程语言 > 详细

基数排序-稳定排序

时间:2019-11-19 22:43:49      阅读:102      评论:0      收藏:0      [点我收藏+]

时间复杂度: o(d(n+r) ) d:执行分桶操作多少次 n:有多少数字要进行排序 r:合并桶的次数

def radix_sort(arr):
loop_times = len(str(max(arr)))
print("times:",loop_times)
for i in range(loop_times):
result = []
for j in range(10):
result.append([])
#result=[[],[],[],[],[],[],[],[],[],[]]
# print("arr:",arr)
for j in arr: #j是个数字
num = j//10**i%10 #i=0,获取个位数 #i=1,获取的是十位数 #i=2,获取的是百位上的数 #.......
result[num].append(j)
arr=[]
for j in result:
arr+=j
return arr

print(radix_sort([5,1,11,12,15,19,101,100]))

基数排序-稳定排序

原文:https://www.cnblogs.com/testerWangzhao/p/11892681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!