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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from random import randint
import time


def revers_bubble(mass):
for i in range(len(mass) - 1):
plase = True
for j in range(len(mass) - 1, i, -1):
if mass[j] < mass[j - 1]:
mass[j], mass[j - 1] = mass[j - 1], mass[j]
plase = False
if plase:
break
return mass


def revers_bubble_new(mass):
for i in range(len(mass) - 1):
plase = True
for j in range(len(mass) - 1, i, -1):
if mass[j] < mass[j - 1]:
mass[j], mass[j - 1] = mass[j - 1], mass[j]
plase = False
if plase:
break
return mass


len_my_mass = int(input('Введите длинну массива: '))
massive_1 = [randint(1, 20) for _ in range(len_my_mass)]
massive_2 = list(massive_1)
print(massive_1)
start = time.time()
massive_1 = revers_bubble(massive_1)
print(f'Работа пузырька без улучшения: {time.time() - start}')

start = time.time()
massive_2 = revers_bubble(massive_2)
print(f'Работа пузырька c улучшением: {time.time() - start}')

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

давайте сделаем итоговый вывод, есть ли смысл модифицировать пузырек?

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from random import random


def merger(mass):
if len(mass) < 2:
return mass[:]
else:
left_mass = merger(mass[:len(mass) // 2])
right_mass = merger(mass[len(mass) // 2:])
return merger_help(left_mass, right_mass)


def merger_help(left_side, right_side):
answ = []
left_idx = 0
right_idx = 0
while left_idx < len(left_side) and right_idx < len(right_side):
if left_side[left_idx] < right_side[right_idx]:
answ.append(left_side[left_idx])
left_idx += 1
elif left_side[left_idx] > right_side[right_idx]:
answ.append(right_side[right_idx])
right_idx += 1
else:
answ.append(right_side[right_idx])
answ.append(left_side[left_idx])
right_idx += 1
left_idx += 1
while left_idx < len(left_side):
answ.append(left_side[left_idx])
left_idx += 1
while right_idx < len(right_side):
answ.append(right_side[right_idx])
right_idx += 1
return answ


def rand(len_mass):
right = 0
left = 50
mass = [float(random() * (right - left)) + left for _ in range(len_mass)]
return mass


len_my_mass = int(input('Введите длинну массива: '))
massive = rand(len_my_mass)
print(massive)
massive = merger(massive)
print(massive)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

слияние реализовано