Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Урок 7. Практическое задание/task_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,52 @@
Подсказка: обратите внимание, сортируем не по возрастанию, как в примере,
а по убыванию
"""

from random import random
import timeit

def bubble(list):
for i in range(9):
for j in range(9 - i):
if list[j] < list[j + 1]:
list[j + 1], list[j] = list[j], list[j + 1]


N = 10
list = [0] * N

for i in range(10):
list[i] = int(random() * 100)

print(f'Исходный массив: {list}')

bubble(list)
print(f'Отсортированный массив: {list}')

# Время выполнение функции - 0.010304400000000002

def bubble_2(list_2):
for i in range(9):
for j in range(9 - i):
if list_2[j] < list_2[j + 1]:
list_2[j + 1], list_2[j] = list_2[j], list_2[j + 1]
else:
break

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вы уверены в правильности оптимизации?
если элемент больше последующего, то делаем break?
не очень понятна оптимизация


N = 10
list_2 = [0] * N

for i in range(10):
list_2[i] = int(random() * 100)

print(f'Исходный массив: {list_2}')

bubble(list_2)
print(f'Отсортированный массив: {list_2}')

print(timeit.timeit("bubble(list)", setup="from __main__ import bubble, list", number=1000))
print(timeit.timeit("bubble_2(list_2)", setup="from __main__ import bubble_2, list_2", number=1000))

# Время выполнения функции - 0.004216400000000002. Таким образом получаем время выполнения после доработки
# в 2 раза быстрее.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

после 1 number массив уже отсортирован

46 changes: 46 additions & 0 deletions Урок 7. Практическое задание/task_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,49 @@
Исходный - [46.11436617832828, 41.62921998361278, 18.45859540989644, 12.128870723745806, 8.025098788570562]
Отсортированный - [8.025098788570562, 12.128870723745806, 18.45859540989644, 41.62921998361278, 46.11436617832828]
"""

import random
import timeit

max_number = 5

def sort(array):

if len(array) < 2:
return array


mid = len(array) // 2

left_part = array[:mid]
right_part = array[mid:]

left_part = sort(left_part)
right_part = sort(right_part)

return list(left_part, right_part)


def list(list_1, list_2):
result = []
x = 0
y = 0
while x < len(list_1) and y < len(list_2):
if list_1[x] <= list_2[y]:
result.append(list_1[x])
x += 1
else:
result.append(list_2[y])
y += 1

result += list_1[x:]
result += list_2[y:]
return result


numbers = [random.uniform(0, 50) for _ in range(max_number)]

print('Исходный список :', numbers)
print('Отсортированный :', (sort(numbers)))


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

слияние реализовали по собственному подходу