import random # 直接插入排序 def insert_sort(L): for x in range(1,len(L)): for i in range(x-1,-1,-1): # range 的步长为负数,则为倒序输出 if L[i] > L[i+1]: tempt = L[i+1] L[i+1] = L[i] L[i] = tempt print(L) def main(): L = [1, 2, 4, 5, 7, 8, 10, 24, 58, 55, 90] random.shuffle(L) # 将数据进行打散 print(L) insert_sort(L) if __name__ == ‘__main__‘: main()
原文:https://www.cnblogs.com/yang-lei/p/14477937.html