-
Notifications
You must be signed in to change notification settings - Fork 77
Lesson_7 #603
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
kalenikai
wants to merge
1
commit into
DmitryChitalov:master
Choose a base branch
from
kalenikai:Lesson_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
Lesson_7 #603
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,61 @@ | ||
| """ | ||
| 1. Отсортируйте по убыванию методом "пузырька" одномерный целочисленный массив, | ||
| заданный случайными числами на промежутке [-100; 100). Выведите на экран | ||
| исходный и отсортированный массивы. Сортировка должна быть реализована в | ||
| виде функции. Обязательно доработайте алгоритм (сделайте его умнее). | ||
|
|
||
| Идея доработки: если за проход по списку не совершается ни одной сортировки, | ||
| то завершение | ||
| Обязательно сделайте замеры времени обеих реализаций | ||
| и обосновать дала ли оптимизация эффективность | ||
|
|
||
| Подсказка: обратите внимание, сортируем не по возрастанию, как в примере, | ||
| а по убыванию | ||
| """ | ||
| """Сортировка пузырьком""" | ||
|
|
||
| import timeit | ||
|
|
||
| # исходный массив | ||
| orig_list = [71, -7, 97, -51, -76, -39, -41, -77, -21, -30, -97, -9, 12, 92, -47, 33, 79, 70, -90, -79] | ||
| def b_sort_desc(lst_obj): | ||
| n = 1 | ||
| while n < len(lst_obj): | ||
| for i in range(len(lst_obj)-n): | ||
| if lst_obj[i] < lst_obj[i+1]: | ||
| lst_obj[i], lst_obj[i+1] = lst_obj[i+1], lst_obj[i] | ||
| n += 1 | ||
| return lst_obj | ||
|
|
||
|
|
||
| # тот же массив, но последние 10 элементов уже отсортированы | ||
| orig_list_presorted = [71, -7, 97, -21, -9, 12, 92, 33, 79, 70, -30, -39, -41,-47, -51, -76, -77, -79, -90, -97] | ||
| def b_sort_desc_v2(lst_obj): | ||
| n = 1 | ||
| while n < len(lst_obj): | ||
| is_sorted = False | ||
| for i in range(len(lst_obj)-n): | ||
| if lst_obj[i] < lst_obj[i+1]: | ||
| lst_obj[i], lst_obj[i+1] = lst_obj[i+1], lst_obj[i] | ||
| is_sorted = True | ||
| if not is_sorted: | ||
| break | ||
| n += 1 | ||
| return lst_obj | ||
|
|
||
| print(f'Source list\n {orig_list}') | ||
| print(f'With no optimization\n {b_sort_desc(orig_list)}') | ||
| print(f'Source list partly presorted\n {orig_list_presorted}') | ||
| print(f'With optimization\n {b_sort_desc_v2(orig_list_presorted)}') | ||
|
|
||
|
|
||
| print(timeit.timeit("b_sort_desc(orig_list)", \ | ||
| setup="from __main__ import b_sort_desc, orig_list", number=100)) | ||
|
|
||
| print(timeit.timeit("b_sort_desc_v2(orig_list_presorted)", \ | ||
| setup="from __main__ import b_sort_desc_v2, orig_list_presorted", number=100)) | ||
|
|
||
| """ | ||
| Без оптимизации 0.0019780999999999965 | ||
| С оптимизацией 0.00015790000000000248 | ||
| """ | ||
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,51 @@ | ||
| """ | ||
| 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 timeit | ||
| import random | ||
|
|
||
|
|
||
| def merge_sort(lst_obj): | ||
| if len(lst_obj) > 1: | ||
| center = len(lst_obj) // 2 | ||
| left = lst_obj[:center] | ||
| right = lst_obj[center:] | ||
|
|
||
| merge_sort(left) | ||
| merge_sort(right) | ||
|
|
||
| i, j, k = 0, 0, 0 | ||
| while i < len(left) and j < len(right): | ||
| if left[i] < right[j]: | ||
| lst_obj[k] = left[i] | ||
| i += 1 | ||
| else: | ||
| lst_obj[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| while i < len(left): | ||
| lst_obj[k] = left[i] | ||
| i += 1 | ||
| k += 1 | ||
|
|
||
| while j < len(right): | ||
| lst_obj[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
| return lst_obj | ||
|
|
||
| orig_list = [random.random() * 50 for _ in range(100)] | ||
| print(f'Source array {orig_list}') | ||
| print(f'Sorted array {merge_sort(orig_list[:])}') | ||
| # замеры | ||
| print(timeit.timeit("merge_sort(orig_list)", \ | ||
| setup="from __main__ import merge_sort, orig_list", number=1)) | ||
|
|
||
|
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. слияние реализовано, как в примере |
||
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,58 @@ | ||
| """ | ||
| 3. Массив размером 2m + 1, где m – натуральное число, заполнен случайным образом. | ||
| Найдите в массиве медиану. Медианой называется элемент ряда, делящий его на | ||
| две равные части: в одной находятся элементы, которые не меньше медианы, | ||
| в другой – не больше медианы. | ||
|
|
||
| Задачу можно решить без сортировки исходного | ||
| массива. | ||
|
|
||
| Но если это слишком сложно, то используйте метод сортировки, | ||
| который не рассматривался на уроках: Шелла, Гномья, ... | ||
|
|
||
| arr[m] | ||
| """ | ||
| import random as rnd | ||
| from statistics import median | ||
| from timeit import timeit | ||
|
|
||
| src = [rnd.randint(1, 100) for _ in range(1, 6000)] | ||
|
|
||
| def find_med_v1(arr): | ||
| return median(src) | ||
|
|
||
| def find_med_v2(arr): | ||
| return sorted(arr)[len(arr) // 2] | ||
|
|
||
| def find_med_v3(arr): | ||
| cnt_ge, cnt_le = 0, 0 | ||
| i, j = 0, 0 | ||
| ln = len(arr) | ||
| while i < ln: | ||
| for j in range(0, ln): | ||
| if i == j: | ||
| continue | ||
| if arr[i] >= arr[j]: | ||
| cnt_ge +=1 | ||
| if arr[i] <= arr[j]: | ||
| cnt_le +=1 | ||
| if cnt_ge >= ln // 2 and cnt_le >= ln // 2: | ||
| return arr[i] | ||
| else: | ||
| i += 1 | ||
| cnt_ge, cnt_le = 0, 0 | ||
| continue | ||
|
|
||
| print(find_med_v1(src)) | ||
| print(find_med_v2(src)) | ||
| print(find_med_v3(src)) | ||
|
|
||
| print(timeit("find_med_v1(src)", setup="from __main__ import find_med_v1, src", number=1)) | ||
| print(timeit("find_med_v2(src)", setup="from __main__ import find_med_v2, src", number=1)) | ||
| print(timeit("find_med_v3(src)", setup="from __main__ import find_med_v3, src", number=1)) | ||
|
|
||
| """ | ||
| Fastest way using "median" from "statistics" 0.0006178999999999768 | ||
| Litlle bit worse using "sorted" 0.0006308000000000147 | ||
| My realization is nightmare :) 0.058788599999999996 | ||
| """ | ||
|
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.
неправильно сделанные замеры
то, о чем мы говорили на уроке....