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