Advertisement
farrismp

Bubble Sort

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