-
Notifications
You must be signed in to change notification settings - Fork 0
lesson7 #7
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
b0rsh3c
wants to merge
1
commit into
master
Choose a base branch
from
lesson7
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
lesson7 #7
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,23 @@ | ||
| """ | ||
| 1. Отсортируйте по убыванию методом "пузырька" одномерный целочисленный массив, | ||
| заданный случайными числами на промежутке [-100; 100). Выведите на экран | ||
| исходный и отсортированный массивы. Сортировка должна быть реализована в | ||
| виде функции. По возможности доработайте алгоритм (сделайте его умнее). | ||
| """ | ||
| import random | ||
| r = [random.randint(-100, 100) for _ in range(30)] | ||
| print(r) | ||
|
|
||
| def bubble_sort(array): | ||
| for i in range(len(array) - 1, 0, -1): | ||
| flag = True | ||
| for n in range(i): | ||
| if array[n] > array[n+1]: | ||
| array[n], array[n+1] = array[n+1], array[n] | ||
| flag = False | ||
|
|
||
| if flag == True: | ||
| break | ||
| return array | ||
|
|
||
| print(bubble_sort(r)) | ||
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,41 @@ | ||
| """ | ||
| 2. Отсортируйте по возрастанию методом слияния одномерный вещественный массив, | ||
| заданный случайными числами на промежутке [0; 50). Выведите на экран исходный | ||
| и отсортированный массивы. | ||
| """ | ||
| import random | ||
| numbers = [random.randint(-100, 100) for _ in range(10)] | ||
| def merge_sort(nums): | ||
| if len(nums) > 1: | ||
| center = len(nums) // 2 | ||
| left = nums[:center] | ||
| right = nums[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]: | ||
| nums[k] = left[i] | ||
| i += 1 | ||
| else: | ||
| nums[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| while i < len(left): | ||
| nums[k] = left[i] | ||
| i += 1 | ||
| k += 1 | ||
|
|
||
| while j < len(right): | ||
| nums[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
| return nums | ||
|
|
||
|
|
||
| print('Исходный массив:', numbers) | ||
| print('Отсортированный массив:', merge_sort(numbers[:])) | ||
|
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,24 @@ | ||
| """ | ||
| 3. Массив размером 2m + 1, где m – натуральное число, заполнен случайным образом. | ||
| Найдите в массиве медиану. Медианой называется элемент ряда, делящий его на | ||
| две равные части: в одной находятся элементы, которые не меньше медианы, | ||
| в другой – не больше медианы. Задачу можно решить без сортировки исходного | ||
| массива. Но если это слишком сложно, то используйте метод сортировки, | ||
| который не рассматривался на уроках | ||
| """ | ||
|
|
||
|
|
||
|
|
||
| numbers = [2, 8, 5, 1, 4] | ||
|
|
||
|
|
||
| def median(nums): | ||
| half = len(nums) // 2 | ||
| nums.sort() | ||
| if not len(nums) % 2: | ||
| return (nums[half - 1] + nums[half]) / 2 | ||
| return nums[half] | ||
|
|
||
|
|
||
| print('Исходный массив:', numbers) | ||
| print('Медиана:', median(numbers[:])) | ||
|
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.
Реализован пузырек с доработкой