Advertisement
GCK

GCK/ bubble sort

GCK
Sep 28th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. my_list=[3,70,4,8,49,4,2,67,0,5,79,2]
  2.  
  3. def swapme(my_list):
  4.     for i in range(len(my_list)-1):
  5.             if my_list[i+1]< my_list[i]:
  6.                 temporary=my_list[i+1]
  7.                 my_list[i+1]=my_list[i]
  8.                 my_list[i]=temporary
  9.     return my_list
  10.    
  11.  
  12. def bubble_sorted(my_list):
  13.     sorted = my_list[:]
  14.     toSort = []
  15.     toSort.append(sorted)
  16.     swapped=True
  17.    
  18.     while swapped==True:
  19.        
  20.         swapme(my_list)
  21.         swapped=my_list[:]
  22.         toSort.append(swapped)
  23.         print(my_list)
  24.        
  25.         swapped=True
  26.            
  27.         if toSort[-1]==toSort[-2]:
  28.             swapped=False
  29.                
  30.                
  31.     return toSort
  32.            
  33. bubble_sorted(my_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement