`

用Python写插入排序

阅读更多

 

 

#插入排序
#定义一个数组
a = [34, 8, 64, 51, 32, 21]
#插入排序算法实现
for i in range(1, len(a)):
	print a
	print 'i is ', i
	tmp = a[i]
	print 'tmp is ', tmp
	b = range(1, i + 1)
	b.reverse()
	for c in b:
		print 'c is ', c, 'a[c] is ', a[c], 'a[c - 1] is ', a[c-1]
		if a[c] < a[c-1]:
			a[c] = a[c - 1]
			a[c-1] = tmp

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics