python插入排序
我使用python语言完成了插入排序这个算法,首先建立一个空的列表,导入random模块生成随机数并添加到列表中去,下面就是具体的插入排序的过程,从下表为1的元素开始,与前面的有序数进行比较,找到合适的地方并进行插入
import random
temp=[]
for i in range(10):
temp.append(random.randint(0,100))
for i in range(1,len(temp)):
key = temp[i]
j = i - 1
while j >= 0 and temp[j] > key:
temp[j + 1] = temp[j]
j = j - 1
temp[j + 1] = key
for i in temp:
print(i)