Advertisement
rickzman

Untitled

Dec 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. # Bubble sorting
  2.  
  3. my_list = [5,9,5,8,1,3]
  4.  
  5. def bubble_sort(sorted):
  6. swap = True
  7. count = 0
  8. swaps = 0
  9. while swap:
  10. swap = False
  11. for i in range(len(sorted) - 1):
  12. count += 1
  13. if sorted[i] > sorted[i + 1]:
  14. sorted[i], sorted[i + 1] = sorted[i + 1], sorted[i]
  15. swaps += 1
  16. swap = True
  17. return (sorted, count, swaps)
  18.  
  19. unsorted = my_list
  20. sorted, count, swaps = bubble_sort(unsorted)
  21. print("The sorted list", sorted)
  22. print("Total iterations", count)
  23. print("Total swaps", swaps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement