''' 插入排序:經過構建有序序列,對於未排序的數據, 在已排序列中從後向前掃描,找到相應的位置並插入 ''' import time start = time.time() def insert_sort(alist): # i 從第二個開始遍歷[9,8,7,6,5,4,3,2,1] for i in range(1, len(alist)): #比較順序,[10.9];[9,10,8];[9,8,10];[8,9,10] for j in range(i, 0, -1): if alist[j] < alist[j-1]: alist[j], alist[j-1] = alist[j-1], alist[j] if __name__ =='__main__': alist = [10,9,8,7,6,5,4,3,2,1] insert_sort(alist) print(alist) end = time.time() print(end-start)