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 467B

1 year ago
1234567891011121314151617181920
  1. def pop_sort(lst):
  2. for i in range(len(lst)-1, 0, -1):
  3. move_max(lst, i)
  4. def move_max(lst, max_index):
  5. for i in range(max_index):
  6. if lst[i] > lst[i+1]:
  7. lst[i], lst[i+1] = lst[i+1], lst[i]
  8. if __name__ == '__main__':
  9. lst = [4, 1, 7, 2, 3, 6]
  10. # pop_sort(lst)
  11. # print(lst)
  12. for j in range(len(lst) - 1, 0, -1):
  13. for i in j:
  14. if lst[i] > lst[i+1]:
  15. lst[i], lst[i+1] = lst[i+1], lst[i]