欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用Python写插入排序

程序员文章站 2022-03-08 16:56:11
...

 

 

#插入排序
#定义一个数组
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