Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #bubble sort example, prints each line of the list as it becomes sorted.
- import time
- my_list = [3, 5, 78, 1, 15, 25, 99, 8] #list requiring sorting
- print("original list was ",(my_list)) #print unsorted list
- def bubble(unsorted):
- length = len(unsorted) - 1
- sorted = False
- while not sorted:
- sorted = True
- for i in range(length):
- if unsorted[i] > unsorted[i+1]: #if first item is bigger than second then it needs sorting
- sorted = False
- unsorted[i], unsorted[i+1] = unsorted[i+1], unsorted[i] #move the first item after the second item & repeat
- print (my_list)
- time.sleep(1)
- bubble(my_list)
- print("sorted list is now ",(my_list)) #print the final sorted list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement