python 列表插入排序
def insert_sort(l:list): for j in range(1,len(l)): if l[j-1]<l[j]: continue p = l[j] for k in range(j-1,-1,-1): if l[k]>p: l[k+1]=l[k] else: k += 1 break l[k]=p return l import random p=list(range(10)) random.shuffle(p) p [2, 7, 0, 4, 8, 5, 6, 3, 1, 9] insert_sort(p) p
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]