bobhig

Bubble Sort

Feb 9th, 2019
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Bubble Sort Function
  2.  
  3. import random
  4.  
  5.  
  6. def bubble_sort(data):
  7.     swapped_flag = True
  8.     length = len(data)
  9.  
  10.     while swapped_flag:
  11.         swapped_flag = False
  12.         for i in range(length-1):
  13.             if data[i] > data[i+1]:
  14.                 data[i], data[i+1] = data[i+1], data[i]
  15.                 swapped_flag = True
  16.     return (data)
  17.  
  18.  
  19. scores = []
  20. sorted_scores = []
  21.  
  22. for x in range (0, 30):
  23.   scores.append(random.randint(0, 10))
  24.  
  25. print(scores)
  26. sorted_scores = bubble_sort(scores)
  27. print(sorted_scores)
RAW Paste Data