li = [3, 4, 2, 9, 1, 7, 6, 5, 8] def insert_sort(data): for i in range(1, len(data)): tmp = data[i] # 拿到手里的数 j = i - 1 # 开始往前比较的开始位 while j >= 0 and data[j] > tmp: # 迭代前进,前后比较 data[j+1] = data[j] j -= 1 data[j+1] = tmp print(data) if __name__ == '__main__': insert_sort(li)