Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- l = []
- for i in range(20000):
- l.append( random.randint(1,1000) )
- print("Unsorted: ",l)
- print("min: ",min(l))
- print("max: ",max(l))
- def bouble(l):
- sorted = False
- end = len(l)-1
- c=0
- while not sorted:
- sorted = True
- for i in range(end):
- c=c+1
- if l[i] > l[i+1]:
- l[i] , l[i+1] = l[i+1] , l[i]
- sorted = False
- end=end-1
- #print("Sorted bouble: ", l)
- print(f"Bouble Выполнило цикл {c} раз")
- #return l
- def shaker(l):
- sorted = False
- start = 0
- end = len(l)-1
- c=0
- while not sorted:
- sorted = True
- for i in range(start, end):
- c=c+1
- if l[i] > l[i+1]:
- l[i] , l[i+1] = l[i+1] , l[i]
- sorted = False
- for i in range(end-1, start,-1):
- c=c+1
- if l[i] < l[i-1]:
- l[i] , l[i-1] = l[i-1] , l[i]
- sorted = False
- start=start+1
- end=end-1
- #print("Shaker Sorted: ", l)
- print(f"Shaker Выполнило цикл {c} раз")
- #return l
- def check(l, fun):
- t = time.time()
- fun(l)
- print("Времени было потрачено: ",time.time() - t)
- l1 = l.copy()
- l2 = l.copy()
- check(l1,bouble)
- check(l2,shaker)
- t=time.time()
- l.sort()
- print("Времени было потрачено: ",time.time() - t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement