-
Notifications
You must be signed in to change notification settings - Fork 77
ДЗ к 7 уроку #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Chilly-WIlly-TD
wants to merge
1
commit into
DmitryChitalov:master
Choose a base branch
from
Chilly-WIlly-TD:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ДЗ к 7 уроку #579
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """ | ||
| 1. Отсортируйте по убыванию методом "пузырька" одномерный целочисленный массив, | ||
| заданный случайными числами на промежутке [-100; 100). Выведите на экран | ||
| исходный и отсортированный массивы. Сортировка должна быть реализована в | ||
| виде функции. Обязательно доработайте алгоритм (сделайте его умнее). | ||
| Идея доработки: если за проход по списку не совершается ни одной сортировки, то завершение | ||
| Обязательно сделайте замеры времени обеих реализаций | ||
| Подсказка: обратите внимание, сортируем не по возрастанию, как в примере, | ||
| а по убыванию | ||
| """ | ||
|
|
||
|
|
||
| # from random import randint | ||
| import random | ||
| import timeit | ||
|
|
||
|
|
||
| def bubble_sort(original_list): | ||
| n = 1 | ||
| while n < len(original_list): | ||
| for i in range(len(original_list) - n): | ||
| if original_list[i] > original_list[i + 1]: | ||
| original_list[i], original_list[i + 1] = original_list[i + 1], original_list[i] | ||
| n += 1 | ||
| return original_list | ||
|
|
||
|
|
||
| "Доробатываем алгоритм" | ||
| def revert_bubble(original_list_2): | ||
| n = 1 | ||
| t = 0 | ||
| while n < len(original_list_2): | ||
| for i in range(len(original_list_2) - n): | ||
| if original_list_2[i] < original_list_2[i + 1]: | ||
| original_list_2[i], original_list_2[i + 1] = original_list_2[i + 1], original_list_2[i] | ||
| t = 1 | ||
| if t == 0: | ||
| break | ||
| n += 1 | ||
| return original_list_2 | ||
|
|
||
| original_list = [random.randint(-100, 100) for _ in range(100)] | ||
| print(f'Исходные массивы:\n{original_list}\n') | ||
|
|
||
| original_list_2 = [random.randint(-100, 100) for _ in range(100)] | ||
| print(f'Сортированные массивы:\n{original_list_2}\n') | ||
|
|
||
| print(timeit.timeit("bubble_sort(original_list)", setup="from __main__ import bubble_sort, original_list", number=1000)) | ||
| print(timeit.timeit("revert_bubble(original_list_2)", setup="from __main__ import revert_bubble, original_list_2", number=1000)) | ||
|
|
||
| """Результат замеров | ||
| original_list: 0.2859691070043482 | ||
| original_list_2: 0.00576216698391363 | ||
| По этим данным можно сделать вывод, что доработка показала эффективность""" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| 2. Отсортируйте по возрастанию методом слияния одномерный вещественный массив, | ||
| заданный случайными числами на промежутке [0; 50). Выведите на экран исходный | ||
| и отсортированный массивы. | ||
| Пример: | ||
| Введите число элементов: 5 | ||
| Исходный - [46.11436617832828, 41.62921998361278, 18.45859540989644, 12.128870723745806, 8.025098788570562] | ||
| Отсортированный - [8.025098788570562, 12.128870723745806, 18.45859540989644, 41.62921998361278, 46.11436617832828] | ||
| """ | ||
|
|
||
|
|
||
| import numpy | ||
| import timeit | ||
|
|
||
|
|
||
| def merge_sort(orig_list): | ||
| to_be_sorted = orig_list[:] | ||
| if len(orig_list) > 1: | ||
| center = len(orig_list) // 2 | ||
| left = orig_list[:center] | ||
| right = orig_list[center:] | ||
|
|
||
| merge_sort(left) | ||
| merge_sort(right) | ||
|
|
||
| i = 0 | ||
| j = 0 | ||
| k = 0 | ||
|
|
||
| while i < len(left) and j < len(right): | ||
| if left[i] < right[j]: | ||
| orig_list[k] = left[i] | ||
| i += 1 | ||
| else: | ||
| orig_list[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| while i < len(left): | ||
| orig_list[k] = left[i] | ||
| i += 1 | ||
| k += 1 | ||
|
|
||
| while j < len(right): | ||
| orig_list[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
| return to_be_sorted | ||
|
|
||
| orig_list = [numpy.random.uniform(0.000000000000000, 50.000000000000000) for _ in range(5)] | ||
| print(f"Исходный массив:\n{orig_list}\n") | ||
| print(f"Отсортированный массив:\n{merge_sort(orig_list)}\n") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. слияние реализовано |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Денис, а теперь поставьте number = 1
и посмотрите рез-тат замеров