Advertisement
scarygarey

bubble_sort

Jan 12th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1.  
  2. from random import randint
  3.  
  4. def bubble_sort(unsorted):
  5. swapped = True
  6. sorted = unsorted[:]
  7. while swapped is True:
  8. swapped = False
  9. for item in range(len(sorted) - 1):
  10. if sorted[item] > sorted[item+1]:
  11. sorted[item], sorted[item+1] = sorted[item+1], sorted[item] #swaps position of list items
  12. swapped = True
  13. return sorted
  14.  
  15.  
  16.  
  17. my_list = [1, 34, 9, 19,91,5,9,5,8,1,3]
  18.  
  19.  
  20. print("Unsorted List", my_list)
  21. print("Sorted List", bubble_sort(my_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement