1. 程式人生 > >python插入排序演示源碼

python插入排序演示源碼

內容 時間 python 備份 while pytho 地方 也有 urn

工作閑暇時間,把寫內容過程較好的內容段做個備份,下面的內容內容是關於python插入排序演示的內容,應該能對各朋友也有用處。


def insert_sort(t):
for i in xrange(len(t)):
key = t[i]
j = i - 1
while j>-1 and t[j]>key:#如果當前值比上一位小,循環結束
t[j+1] = t[j]
j -= 1
t[j+1] = key #確保待插入值被插入到合適的地方
return t

t = [1,3,2,4]
print insert_sort(t)





python插入排序演示源碼