Advertisement
Gronario

Untitled

Mar 25th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. import random
  2.  
  3. b1=[random.randint(1,1000) for i in range(10)]
  4. b2=[random.randint(1,1000) for i in range(100)]
  5. b3=[random.randint(1,1000) for i in range(1000)]
  6. b4=[random.randint(1,1000) for i in range(2000)]
  7. b5=[random.randint(1,1000) for i in range(3000)]
  8. b6=[random.randint(1,1000) for i in range(4000)]
  9. b7=[random.randint(1,1000) for i in range(8000)]
  10. b8=[random.randint(1,1000) for i in range(12000)]
  11. b9=[random.randint(1,1000) for i in range(50000)]
  12. b10=[random.randint(1,1000) for i in range(100000)]
  13.  
  14.  
  15. arr_len = []
  16. def len_arr_measurement(*arr):
  17.     for i in arr:
  18.         arr_len.append(len(i))
  19.  
  20. len_arr_measurement(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10)
  21. # print(f"arr_len: {arr_len}")
  22.  
  23. arr_time = []
  24. def benchmark(func):
  25.     import time
  26.     def wrapper(arr):
  27.  
  28.         start=time.perf_counter()
  29.         func(arr)
  30.         end=time.perf_counter()
  31.         res=end-start
  32.         arr_time.append(res)
  33.         print(f'Execution time: {res:.5f} секунд')
  34.     return wrapper
  35.  
  36. @benchmark
  37.  
  38. def heap_sort (sequence):
  39.  
  40.     def shift_down (parent, limit):
  41.         item = sequence[parent]
  42.         while True:
  43.             child = (parent << 1) + 1
  44.             if child >= limit:
  45.                 break
  46.             if child + 1 < limit and sequence[child] < sequence[child + 1]: # !
  47.                 child += 1
  48.             if item < sequence[child]:                                      # !
  49.                 sequence[parent] = sequence[child]
  50.                 parent = child
  51.             else:
  52.                 break
  53.         sequence[parent] = item
  54.     # Тело функции heap_sort
  55.     length = len(sequence)
  56.     # Формирование первичной пирамиды
  57.     for index in range((length >> 1) - 1, -1, -1):
  58.         shift_down(index, length)
  59.     # Окончательное упорядочение
  60.     for index in range(length - 1, 0, -1):
  61.         sequence[0], sequence[index] = sequence[index], sequence[0]
  62.         shift_down(0, index)
  63.  
  64.  
  65. z=heap_sort(b1)
  66. z=heap_sort(b2)
  67. z=heap_sort(b3)
  68. z=heap_sort(b4)
  69. z=heap_sort(b5)
  70. z=heap_sort(b5)
  71. z=heap_sort(b6)
  72. z=heap_sort(b7)
  73. z=heap_sort(b8)
  74. z=heap_sort(b9)
  75. z=heap_sort(b10)
  76. print(f"arr_time:",end=" ")
  77. for i in arr_time:
  78.     print(f"{i:.4f}",end=" ")
  79.  
  80. print()
  81. print(f"\narr_len_quantity of elements: {len(arr_len)} \narr_time_quantity: {len(arr_time)} ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement