Advertisement
kevinbocky

bubblesort.py

Jan 27th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #make a bubblesort function
  2. numbers = [1,2,5,8,7,9,12,51,41,87,95,98,992,52,472,21,43]
  3.  
  4.  
  5. def bubblesort(list): #define the bubblesort function
  6. sorted = list[:] #copy of old list(numbers)
  7. sort = True
  8. while sort == True: #while loop start, then sets sort to False, if it has to perform action, sets sort back to True
  9. sort = False #if it has to perform no action,sort stays False, while loop ends
  10. for i in range(len(sorted) - 1):
  11. if sorted[i] > sorted[i+1]:
  12. sorted[i], sorted[i+1] = sorted[i+1], sorted[i]
  13. sort = True #turns sort back to True if action performed
  14.  
  15. print(sorted)
  16.  
  17. bubblesort(numbers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement