-
Notifications
You must be signed in to change notification settings - Fork 76
DZ7 #571
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
base: master
Are you sure you want to change the base?
DZ7 #571
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,91 @@ | |
| Исходный - [46.11436617832828, 41.62921998361278, 18.45859540989644, 12.128870723745806, 8.025098788570562] | ||
| Отсортированный - [8.025098788570562, 12.128870723745806, 18.45859540989644, 41.62921998361278, 46.11436617832828] | ||
| """ | ||
|
|
||
| from random import uniform | ||
| from timeit import timeit | ||
|
|
||
| element = int(input('Введите число элементов:')) | ||
| LST = [uniform(0, 50) for _ in range(element)] | ||
| LST1 = LST[:] | ||
| print(LST) | ||
|
|
||
| ''' | ||
| Это первая версия. Ее я решил написать не заглядывая в ваш пример. | ||
| Она оказалась рабочая, но работала медленнее чем ваша. | ||
| Чем больше был массив, тем больше разница. При массиве | ||
| в 100 тыс она была уже в 3 раз, на 200 в 5, и дальше увеличивалась в разы | ||
| миллион было уже не дождаться. | ||
| Тогда я посмотрел вашу версию и перелелал часть своей. | ||
| Стала летать. | ||
|
|
||
| Возможно моя работала медленнее из-за того что использовал extend и pop | ||
|
|
||
|
|
||
| ''' | ||
|
|
||
|
|
||
| def merger_sort_1(lst): | ||
| center = len(lst) // 2 | ||
| tmp = [] | ||
| left_list = lst[:center] | ||
| right_list = lst[center:] | ||
| if len(left_list) > 1: | ||
| left_list = merger_sort_1(left_list) | ||
| if len(right_list) > 1: | ||
| right_list = merger_sort_1(right_list) | ||
|
|
||
| while True: | ||
| if len(left_list) == 0: | ||
| tmp.extend(right_list) | ||
| break | ||
| elif len(right_list) == 0: | ||
| tmp.extend(left_list) | ||
| break | ||
| if left_list[0] < right_list[0]: | ||
| tmp.append(left_list.pop(0)) | ||
| else: | ||
| tmp.append(right_list.pop(0)) | ||
| return tmp | ||
|
|
||
|
|
||
| def merger_sort_2(lst): | ||
| center = len(lst) // 2 | ||
| left_list = lst[:center] | ||
| right_list = lst[center:] | ||
| if len(left_list) > 1: | ||
| left_list = merger_sort_2(left_list) | ||
| if len(right_list) > 1: | ||
| right_list = merger_sort_2(right_list) | ||
|
|
||
| i, j, k = 0, 0, 0 | ||
| while i < len(left_list) and j < len(right_list): | ||
| if left_list[i] < right_list[j]: | ||
| lst[k] = left_list[i] | ||
| i += 1 | ||
| else: | ||
| lst[k] = right_list[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| while i < len(left_list): | ||
| lst[k] = left_list[i] | ||
| i += 1 | ||
| k += 1 | ||
|
|
||
| while j < len(right_list): | ||
| lst[k] = right_list[j] | ||
| j += 1 | ||
| k += 1 | ||
| return lst | ||
|
|
||
|
|
||
| print(timeit("merger_sort_1(LST1)", \ | ||
| setup="from __main__ import merger_sort_1, LST1", number=1)) | ||
|
|
||
| print(merger_sort_1(LST1)) | ||
|
|
||
| print(timeit("merger_sort_2(LST)", \ | ||
| setup="from __main__ import merger_sort_2, LST", number=1)) | ||
|
|
||
| print(merger_sort_2(LST)) | ||
|
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. плюс за свою реализацию в слиянии |
||
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.
вы передаете на второй замер уже отсортированный массив
проверьте
поэтому результаты ниже неверные