码迷,mamicode.com
首页 > 编程语言 > 详细

桶排序算法-python实现

时间:2016-06-07 11:21:11      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:python 桶排序

来源:

<啊哈算法>


5个数字 5 3 5 2 8需要排序.

用到了桶排序思路

l=[0,0,0,0,0,0,0,0,0,0]

p=[5,3,5,2,8]

for i in range(10):
    for j in p:
        if i==j:
            l[i]+=1
print l

new_l=[]
for i in range(10): #核心点在这里
    if l[i]!=0:
        for j in range(l[i]):
            new_l.append(i)
print new_l

简化成:对一下,对一下,如果值不为0,就输出对应的index.如值为1,输出一次该index,如果值为2输入两次值对应的index.

[0, 0, 1, 1, 0, 2, 0, 0, 1, 0]


桶排序算法-python实现

标签:python 桶排序

原文地址:http://lannyma.blog.51cto.com/4544390/1786784

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!