Advertisement
scipiguy

Programming 102: Bubble Sort

Jun 27th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. # Bubble sort algorithm
  2.  
  3. my_list = [5,9,5,8,1,3]
  4.  
  5. def bubble_sort(the_list):
  6.     orig_list = the_list
  7.     swapped = True
  8.     passes = 0
  9.     while swapped == True:
  10.         swapped = False
  11.         for x in range(len(the_list)-1):
  12.             if the_list[x] > the_list[x+1]:
  13.                 temp = the_list[x]
  14.                 the_list[x] = the_list[x+1]
  15.                 the_list[x+1] = temp
  16.                 swapped = True
  17.         passes += 1
  18.         print("Pass "+ str(passes) + ": " + str(the_list))
  19.     return the_list
  20.  
  21. print("Bubble sorting\n==============")
  22. print("Your unsorted list is " + str(my_list))    
  23. sorted = bubble_sort(my_list)
  24. print("Your sorted list is " + str(sorted))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement