Advertisement
Lonely_Wanderer

Сортировки говна

Nov 29th, 2023
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. import random
  2. import time
  3. l = []
  4.  
  5. for i in range(20000):
  6.     l.append( random.randint(1,1000) )
  7.  
  8. print("Unsorted: ",l)
  9. print("min: ",min(l))
  10. print("max: ",max(l))
  11.  
  12.  
  13. def bouble(l):
  14.     sorted = False
  15.     end = len(l)-1
  16.     c=0
  17.     while not sorted:
  18.         sorted = True
  19.         for i in range(end):
  20.             c=c+1
  21.             if l[i] > l[i+1]:
  22.                 l[i] , l[i+1] = l[i+1] , l[i]
  23.                 sorted = False
  24.         end=end-1
  25.     #print("Sorted bouble: ", l)
  26.     print(f"Bouble Выполнило цикл {c} раз")
  27.     #return l
  28.  
  29.  
  30.  
  31.  
  32. def shaker(l):
  33.     sorted = False
  34.     start = 0
  35.     end = len(l)-1
  36.     c=0
  37.     while not sorted:
  38.         sorted = True
  39.         for i in range(start, end):
  40.             c=c+1
  41.             if l[i] > l[i+1]:
  42.                 l[i] , l[i+1] = l[i+1] , l[i]
  43.                 sorted = False
  44.         for i in range(end-1, start,-1):
  45.             c=c+1
  46.             if l[i] < l[i-1]:
  47.                 l[i] , l[i-1] = l[i-1] , l[i]
  48.                 sorted = False
  49.         start=start+1
  50.         end=end-1
  51.     #print("Shaker Sorted: ", l)
  52.     print(f"Shaker Выполнило цикл {c} раз")
  53.     #return l
  54.  
  55.  
  56. def check(l, fun):
  57.     t = time.time()
  58.     fun(l)
  59.     print("Времени было потрачено: ",time.time() - t)
  60.  
  61. l1 = l.copy()
  62. l2 = l.copy()
  63.  
  64. check(l1,bouble)
  65. check(l2,shaker)
  66. t=time.time()
  67. l.sort()
  68. print("Времени было потрачено: ",time.time() - t)
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement