AberdeenAngus

bubble sort

Aug 30th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. my_list = [4,3, 8,15,9,5,8,1,3]
  2.  
  3. def bubblesort(unsorted):
  4. toSort = unsorted[:]
  5. swapped = True
  6. while swapped == True:
  7. swapped = False
  8. for i in range(len(toSort)-1):
  9. if toSort[i] > toSort[i+1]:
  10. toSort[i], toSort[i+1] = toSort[i+1], toSort[i]
  11. swapped = True
  12. return toSort
  13.  
  14. print(my_list)
  15. print(bubblesort(my_list))
Advertisement
Add Comment
Please, Sign In to add comment