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