Advertisement
KaySawbridge

Bubble sort function

Jul 31st, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. #Bubble sort
  2. my_list = [100,6,3,21,76,1,99,88,22,54,45,7,9,11]
  3.  
  4. #Bubble sort function
  5. def bubble_sort(unsorted_list):
  6.     sorted_list=unsorted_list[:] #Make a copy of the original unsorted list
  7.     swapped = True
  8.     while swapped:
  9.         swapped=False
  10.         for i in range(len(sorted_list) - 1):
  11.             if sorted_list[i] > sorted_list[i+1]:  #go throught the copy of the list and check if each number is smalled than the one after it
  12.                 sorted_list[i],sorted_list[i+1] = sorted_list[i+1], sorted_list[i]  #if the number is larger then swap the places
  13.                 swapped = True
  14.     return sorted_list
  15.        
  16.    
  17. print(bubble_sort(my_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement