Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Bubble sorting
- my_list = [5,9,5,8,1,3]
- def bubble_sort(sorted):
- swap = True
- count = 0
- swaps = 0
- while swap:
- swap = False
- for i in range(len(sorted) - 1):
- count += 1
- if sorted[i] > sorted[i + 1]:
- sorted[i], sorted[i + 1] = sorted[i + 1], sorted[i]
- swaps += 1
- swap = True
- return (sorted, count, swaps)
- unsorted = my_list
- sorted, count, swaps = bubble_sort(unsorted)
- print("The sorted list", sorted)
- print("Total iterations", count)
- print("Total swaps", swaps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement