Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sort_with_bubbles(lst):
- swap_occurred = True
- while swap_occurred:
- swap_occurred = False
- for i in range(len(lst) - 1):
- if lst[i] > lst[i + 1]:
- # The goal of the swap is to store a number to a temporary variable so that we can change that variable and preserve the value. To do that, we need to copy the list item to temp first:
- temp = lst[i]
- lst[i] = lst[i + 1]
- lst[i + 1] = temp
- swap_occurred = True
- return lst
- print(sort_with_bubbles([5, 3, 1, 2, 4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement