Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. from math import floor
  2. from matplotlib import pyplot as plt
  3. import random
  4.  
  5. def merge_sort(lista):
  6. zliczanie = 0
  7. if len(lista)<=1:
  8. return zliczanie
  9.  
  10. lewa = lista[:len(lista)//2]
  11. prawa = lista[len(lista)//2:]
  12.  
  13. merge_sort(lewa)
  14. merge_sort(prawa)
  15. tmp, zliczanie = merge(lewa, prawa)
  16. lista.clear()
  17. lista.extend(tmp)
  18. return zliczanie
  19.  
  20. def merge(lewa, prawa):
  21. zliczanie = 0
  22. wynik = []
  23. while lewa and prawa:
  24. zliczanie += 1
  25. if lewa[0] <= prawa[0]:
  26. zliczanie += 1
  27. wynik.append(lewa.pop(0))
  28. else:
  29. zliczanie += 1
  30. wynik.append(prawa.pop(0))
  31. while lewa:
  32. zliczanie += 1
  33. wynik.append(lewa.pop(0))
  34. while prawa:
  35. zliczanie += 1
  36. wynik.append(prawa.pop(0))
  37. return wynik, zliczanie
  38.  
  39. def start():
  40. wyniki= []
  41. i = 1
  42. for i in range(1000):
  43. lista = list(range(i))
  44. random.shuffle(lista)
  45. wyniki.append(merge_sort(lista))
  46. return wyniki
  47.  
  48. wyniki = start()
  49. print(wyniki)
  50. plt.plot(wyniki)
  51. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement