Advertisement
marfach

bubble sort

Sep 28th, 2022 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. import time
  2.  
  3. n = []
  4. for i in range(int(input("Enter your array length: "))):
  5.     n.append(int(input("Enter some data: ")))
  6.  
  7. print(f"Your array is: {n}")
  8.  
  9. #Bubble sort
  10. start = time.time()
  11. def bubble_sort():
  12.     swp_count = 0
  13.     for run in range(len(n)-1):
  14.         for x in range(len(n)-1) :
  15.             if n[x] > n[x+1]:
  16.                 swp_count += 1
  17.                 n[x], n[x+1] = n[x+1],n[x]
  18.         print(f"Your sorted array is: {n}")
  19.     print(f"Sorted {swp_count} times.")
  20.  
  21. bubble_sort()
  22.  
  23. end = time.time()
  24.  
  25. print(f"Bubble sort was during {end-start} seconds.")
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement