# encoding=utf-8 def insert_sort(alist): """插入排序""" n = len(alist) for j in range(1, n): # 控制将拿到的元素放在前面有序序列正确位置的过程 for i in range(j, 0, -1): # 如果比前面的元素小,则往前移动 if alist[i] < alist[i-1]: alist[i], alist[i-1] = alist[i-1], alist[i] # 否则,代表比前面的所有元素都小, 不需要移动 else: break if __name__ == '__main__': alist = [22, 1, 4, 553, 3, 212, 77] print(alist) insert_sort(alist) print(alist)