-
Notifications
You must be signed in to change notification settings - Fork 77
hw_done #581
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
Kosma4evDmitry
wants to merge
1
commit into
DmitryChitalov:master
Choose a base branch
from
Kosma4evDmitry:les_7
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
hw_done #581
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 |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
|
||
| 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 раза быстрее. | ||
|
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. после 1 number массив уже отсортирован |
||
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 |
|---|---|---|
|
|
@@ -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))) | ||
|
|
||
|
|
||
|
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.
вы уверены в правильности оптимизации?
если элемент больше последующего, то делаем break?
не очень понятна оптимизация