Antypas

Bubble Sort

Apr 17th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. # Bubble Sort Program
  2. import random
  3. my_list = []
  4. for i in range(0,20):
  5.     my_list.append(random.randint(0,10))
  6. #my_list = [5,9,5,8,1,3]
  7.  
  8. def bubble_sort(unsorted):
  9.     sorted = unsorted[:]
  10.     swapped = True
  11.     while swapped == True:
  12.         swapped = False
  13.         for i in range(len(sorted)-1):
  14.             if sorted[i] > sorted[i+1]:
  15.                 sorted[i], sorted[i+1] = sorted[i+1], sorted[i]
  16.                 swapped = True
  17.     return sorted
  18. print(my_list)
  19. print(bubble_sort(my_list))
Add Comment
Please, Sign In to add comment