简单桶排序算法-python实现
1 #-*- coding: UTF-8 -*- 2 import numpy as np 3 4 def BucketSort(a, n): 5 barrel = np.zeros((1, n), dtype = 'int32') 6 for i in xrange(0,a.size): 7 barrel[0][a[i]] += 1 8 k = 0 9 for i in xrange(0,n): 10 if barrel[0][i] != 0: 11 for j in xrange(0,barrel[0][i]): 12 a[k] = i 13 k += 1 14 15 16 if __name__ == '__main__': 17 a = np.random.randint(0, 100, size = 10) 18 print "Before sorting..." 19 print "---------------------------------------------------------------" 20 print a 21 print "---------------------------------------------------------------" 22 BucketSort(a, 100) 23 print "After sorting..." 24 print "---------------------------------------------------------------" 25 print a 26 print "---------------------------------------------------------------"
作者:Alex Yu
出处:http://www.cnblogs.com/biaoyu/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。