-
Notifications
You must be signed in to change notification settings - Fork 77
7 домашняя работа #575
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?
7 домашняя работа #575
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,42 @@ | |
| Подсказка: обратите внимание, сортируем не по возрастанию, как в примере, | ||
| а по убыванию | ||
| """ | ||
|
|
||
| import random | ||
|
|
||
|
|
||
| def bubble_sort(array, reverse=False): | ||
| sign = int(reverse) * 2 - 1 | ||
| n = 1 | ||
|
|
||
| while n < len(array): | ||
| is_sort = True | ||
| for i in range(len(array) - n): | ||
| if array[i] * sign < array[i + 1] * sign: | ||
| array[i], array[i + 1] = array[i + 1], array[i] | ||
| is_sort = False | ||
|
|
||
| if is_sort: | ||
| break | ||
|
|
||
| n += 1 | ||
| print(array) | ||
|
|
||
|
|
||
| SIZE = 10 | ||
| LIMIT = 100 | ||
| data = [random.randrange(-LIMIT, LIMIT) for _ in range(SIZE)] | ||
|
|
||
| print(data) | ||
| bubble_sort(data, reverse=True) | ||
| print(data) | ||
|
|
||
| ''' | ||
| Применена оптимизация естественности поведения. | ||
| На вход дан упорядоченный массив данных, и сортировать его не нужно. | ||
| Работает данная оптимизация с помощью: | ||
| is_sort = True # считается что массив отсортирован | ||
| is_sort = False # если были переставлены элементы, то массив считается не отсортированным | ||
| if is_sort: # в теле цикла проверили были ли изменения, прошли по всему массиву, если всё ок, ты выходим. | ||
| break | ||
| ''' | ||
|
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. давайте сделаем итоговый вывод, есть ли смысл модифицировать пузырек? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,45 @@ | |
| Исходный - [46.11436617832828, 41.62921998361278, 18.45859540989644, 12.128870723745806, 8.025098788570562] | ||
| Отсортированный - [8.025098788570562, 12.128870723745806, 18.45859540989644, 41.62921998361278, 46.11436617832828] | ||
| """ | ||
|
|
||
| import random | ||
|
|
||
|
|
||
| def merge_sort(array): | ||
| if len(array) <= 1: | ||
| return array | ||
|
|
||
| elif len(array) == 2: | ||
| if array[0] > array[1]: | ||
| array[0], array[1] = array[1], array[0] | ||
| return array | ||
|
|
||
| left = merge_sort(array[:len(array) // 2]) | ||
| right = merge_sort(array[len(array) // 2:]) | ||
| i = j = 0 | ||
|
|
||
| while len(left) > i and len(right) > j: | ||
| if left[i] < right[j]: | ||
| array[i + j] = left[i] | ||
| i += 1 | ||
| else: | ||
| array[i + j] = right[j] | ||
| j += 1 | ||
|
|
||
| while len(left) > i: | ||
| array[i + j] = left[i] | ||
| i += 1 | ||
| while len(right) > j: | ||
| array[i + j] = right[j] | ||
| j += 1 | ||
|
|
||
| return array | ||
|
|
||
|
|
||
| SIZE = 10 | ||
| LIMIT = 50 | ||
| data = [random.uniform(0, LIMIT) for _ in range(SIZE)] | ||
|
|
||
| print(data) | ||
| merge_sort(data) | ||
| print(data) | ||
|
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. слияние реализовано |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,33 @@ | |
| массива. Но если это слишком сложно, то используйте метод сортировки, | ||
| который не рассматривался на уроках | ||
| """ | ||
|
|
||
| import random | ||
|
|
||
|
|
||
| def median(array): | ||
| for i in range(len(array)): | ||
| my_min = equal = my_max = 0 | ||
| for j in range(len(array)): | ||
| if array[i] < array[j]: | ||
| my_min += 1 | ||
| elif array[i] > array[j]: | ||
| my_max += 1 | ||
| else: | ||
| equal += 1 | ||
| equal -= 1 | ||
|
|
||
| if my_min == my_max or my_min == equal + my_max or my_max == equal + my_min or \ | ||
| (equal > 1 and abs(my_max - my_min) < equal): | ||
| return array[i] | ||
|
|
||
|
|
||
| SIZE = 4 | ||
| LIMIT = 100 | ||
| data = [random.randrange(0, LIMIT) for _ in range(2 * SIZE + 1)] | ||
|
|
||
| print(data) | ||
| print(f'Медиана = {median(data)}') | ||
| print(data) | ||
| print(sorted(data)) | ||
|
|
||
|
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.
Нина, а зачем на вход подавать упорядоченный массив?
зачем сортировка, если массив уже упорядочен
нужно передавать неупорядоченный