Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- b1=[random.randint(1,1000) for i in range(10)]
- b2=[random.randint(1,1000) for i in range(100)]
- b3=[random.randint(1,1000) for i in range(1000)]
- b4=[random.randint(1,1000) for i in range(2000)]
- b5=[random.randint(1,1000) for i in range(3000)]
- b6=[random.randint(1,1000) for i in range(4000)]
- b7=[random.randint(1,1000) for i in range(8000)]
- b8=[random.randint(1,1000) for i in range(12000)]
- b9=[random.randint(1,1000) for i in range(50000)]
- b10=[random.randint(1,1000) for i in range(100000)]
- arr_len = []
- def len_arr_measurement(*arr):
- for i in arr:
- arr_len.append(len(i))
- len_arr_measurement(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10)
- # print(f"arr_len: {arr_len}")
- arr_time = []
- def benchmark(func):
- import time
- def wrapper(arr):
- start=time.perf_counter()
- func(arr)
- end=time.perf_counter()
- res=end-start
- arr_time.append(res)
- print(f'Execution time: {res:.5f} секунд')
- return wrapper
- @benchmark
- def heap_sort (sequence):
- def shift_down (parent, limit):
- item = sequence[parent]
- while True:
- child = (parent << 1) + 1
- if child >= limit:
- break
- if child + 1 < limit and sequence[child] < sequence[child + 1]: # !
- child += 1
- if item < sequence[child]: # !
- sequence[parent] = sequence[child]
- parent = child
- else:
- break
- sequence[parent] = item
- # Тело функции heap_sort
- length = len(sequence)
- # Формирование первичной пирамиды
- for index in range((length >> 1) - 1, -1, -1):
- shift_down(index, length)
- # Окончательное упорядочение
- for index in range(length - 1, 0, -1):
- sequence[0], sequence[index] = sequence[index], sequence[0]
- shift_down(0, index)
- z=heap_sort(b1)
- z=heap_sort(b2)
- z=heap_sort(b3)
- z=heap_sort(b4)
- z=heap_sort(b5)
- z=heap_sort(b5)
- z=heap_sort(b6)
- z=heap_sort(b7)
- z=heap_sort(b8)
- z=heap_sort(b9)
- z=heap_sort(b10)
- print(f"arr_time:",end=" ")
- for i in arr_time:
- print(f"{i:.4f}",end=" ")
- print()
- 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