Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my_list = [5,9,5,8,1,3]
- #Sorting a list with the method BubbleSort
- #We define a boolean swapped to see if we swapped some items
- #during the checking of the list
- #For changing the values of two neighbour-items we set the variable helpSort
- def bubble_sort(unsorted):
- oldList = unsorted
- swapped = True
- while (swapped == True):
- swapped = False
- for i in range(len(unsorted) - 1):
- if unsorted[i] > unsorted[i + 1]:
- helpSort = unsorted[i]
- unsorted[i] = unsorted[i + 1]
- unsorted[i + 1] = helpSort
- swapped = True
- return unsorted
- sortedList = bubble_sort(my_list)
- print(sortedList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement