You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

插入.py 429B

1 year ago
123456789101112131415161718192021
  1. def insert(lst, index):
  2. if lst[index-1] < lst[index]:
  3. return
  4. tmp = lst[index]
  5. tmp_index = index
  6. while tmp_index > 0 and lst[tmp_index-1] > tmp:
  7. lst[tmp_index] = lst[tmp_index-1]
  8. tmp_index -= 1
  9. lst[tmp_index] = tmp
  10. def insert_sort(lst):
  11. for i in range(1, len(lst)):
  12. insert(lst, i)
  13. if __name__ == '__main__':
  14. lst = [1, 6, 2, 7, 5]
  15. insert_sort(lst)
  16. print(lst)