Advertisement
qtinio

mmmm sortowawnko

May 28th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. def bomble(lista):
  5. start_b = time.time()
  6. bez_zmiany = False
  7. while not bez_zmiany:
  8. for i in range(len(lista)-1):
  9. if lista[i] > lista[i+1]:
  10. lista[i], lista[i+1] = lista[i+1], lista[i]
  11. break
  12. if i == len(lista)-2:
  13. bez_zmiany = True
  14. break
  15. stop_b = time.time()
  16. print(f'Babelkowe: {(stop_b - start_b):.4f}s')
  17.  
  18.  
  19. def wybor(lista):
  20. start_w = time.time()
  21. for i in range(len(lista)):
  22. index = lista.index(min(lista[i:]))
  23. lista[index], lista[i] = lista[i], lista[index]
  24. stop_w = time.time()
  25. print(f'Wybor: {(stop_w - start_w):.4f}s')
  26.  
  27.  
  28. def quicksort(lista, start, stop):
  29. if start >= stop-1:
  30. return
  31.  
  32. pivot = int((stop - start) / 2) + start
  33.  
  34. i = start
  35. j = stop
  36. while i < j:
  37. if lista[i] >= lista[pivot] >= lista[j]:
  38. lista[i], lista[j] = lista[j], lista[i]
  39. j -= 1
  40. i += 1
  41.  
  42. quicksort(lista, start, j)
  43. quicksort(lista, j+1, stop)
  44.  
  45.  
  46. def quick(lista):
  47. start_q = time.time()
  48. quicksort(lista, 0, len(lista)-1)
  49. stop_q = time.time()
  50. print(f'Quicksort: {(stop_q - start_q):.4f}s')
  51.  
  52. if __name__ == "__main__":
  53. rnd = random.sample(range(5000), 20)
  54. rnd_b = rnd.copy()
  55. rnd_w = rnd.copy()
  56. rnd_q = rnd.copy()
  57.  
  58. # bomble(rnd_b)
  59. wybor(rnd_w)
  60. quick(rnd_q)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement