Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my_list = [5,9,5,8,1,3,4,8,2,0,3,6,1,7,9]
- # Making a copy of a list
- unclassified = my_list[:]
- # Create a function for the bubble sort algorithm.
- # With a single parameter, which will be the data to be sorted.
- def bubble_sort(unclassified):
- # Initialise a swapped variable to be True
- changed = True
- # Start a loop that runs until swapped is no longer True
- while changed == True:
- # Change swapped to False
- changed = False
- # Iterate over the unsorted list
- for item in range(len(unclassified) - 1):
- # Compare adjacent items in the list.
- if unclassified[item] > unclassified[item+1]:
- unclassified[item], unclassified[item+1] = unclassified[item+1], unclassified[item] # Swapping list items around
- # Change swapped back to True if a swap has been made
- changed = True
- # Return the ordered list, if no swaps have been made after a full iteration.
- return unclassified
- # Print the list
- print(my_list)
- bubble_sort(unclassified)
- # Print the sorted list
- print(unclassified)
- print ("******************************************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement