Advertisement
FranzVuttke

bubble sort variations

Jan 17th, 2023 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. def bubble(L):
  2.     cnt = 0
  3.     for i in range(len(L)):
  4.         for j in range(len(L)-1):
  5.             if L[j] > L[j+1]:
  6.                 L[j], L[j+1] = L[j+1], L[j]
  7.         cnt += 1
  8.     print("cnt: ", cnt)
  9.     return(L)
  10.    
  11. def bubble_ex(alst):
  12.     swap = True
  13.     cnt = 0
  14.     while swap:
  15.         swap = False
  16.         for i in range(len(alst)-1):
  17.             if alst[i] > alst[i+1]:
  18.                 alst[i], alst[i+1] = alst[i+1], alst[i]
  19.                 swap = True
  20.         cnt += 1
  21.     print("cnt:",cnt)
  22.     return alst
  23.    
  24.    
  25. import random
  26. print(bubble_ex([random.randint(1, 50) for _ in range(65)]))    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement