Advertisement
kenadams53

bubble_sort_some_numbers

Aug 28th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #3.5 (102)Bubble sort Python coded by Ken Adams
  2.  
  3. my_list = [5,9,5,8,1,3] #list to be sorted
  4. print("The unsorted list")
  5. print(my_list)
  6.  
  7. def bubble_sort(any_list):
  8. sorted_list = any_list[:]
  9. swapped = True
  10. while swapped == True:
  11. swapped = False
  12.  
  13. for i in range(len(sorted_list) - 1):
  14. if sorted_list[i] > sorted_list[i+1]:
  15. sorted_list[i], sorted_list[i+1] = sorted_list[i+1], sorted_list[i]
  16. swapped = True
  17.  
  18. return sorted_list
  19.  
  20. sorted = bubble_sort(my_list)
  21. print("\nnow the sorted list")
  22. print(sorted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement