Advertisement
HJ50

bubble_sort

Apr 6th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. #bubble sort
  2.  
  3. def bubble_sort(my_list):
  4.     list_copy=my_list
  5.    
  6.     swapped=True    
  7.     while swapped==True:
  8.         i=0
  9.         swapped=False                           #reset flag
  10.         while i<(len(list_copy)-1):             #first to penultimate value
  11.             if list_copy[i]>list_copy[i+1]:
  12.                 swapped=True
  13.                 temp=list_copy[i]               #switch values
  14.                 list_copy[i]=list_copy[i+1]
  15.                 list_copy[i+1]=temp
  16.             i+=1
  17.            
  18.     return  list_copy  
  19. #test
  20. orig_list=[3,5,7,2,9,4,5,2,7,0]
  21. print(bubble_sort(orig_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement