Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. my_list = [5,9,5,8,1,3]
  2.  
  3. #Sorting a list with the method BubbleSort
  4. #We define a boolean swapped to see if we swapped some items
  5. #during the checking of the list
  6. #For changing the values of two neighbour-items we set the variable helpSort
  7. def bubble_sort(unsorted):
  8.     oldList = unsorted
  9.     swapped = True
  10.     while (swapped == True):
  11.         swapped = False
  12.         for i in range(len(unsorted) - 1):
  13.             if unsorted[i] > unsorted[i + 1]:
  14.                 helpSort = unsorted[i]
  15.                 unsorted[i] = unsorted[i + 1]
  16.                 unsorted[i + 1] = helpSort
  17.                 swapped = True
  18.     return unsorted
  19.  
  20. sortedList = bubble_sort(my_list)
  21. print(sortedList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement