03 插入排序
''' 插入排序:通过构建有序序列,对于未排序的数据, 在已排序列中从后向前扫描,找到相应的位置并插入 ''' 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)