-
Notifications
You must be signed in to change notification settings - Fork 0
Heap Sort + tests #4
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
stuffacc
wants to merge
2
commits into
main
Choose a base branch
from
t5
base: main
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,17 @@ | ||
| # .github/workflows/simple-test.yml | ||
| name: Simple Test | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ heap_sort ] | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.12.3' | ||
| - run: pip install pytest | ||
| - run: pytest test_heap_sort.py -v |
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,32 @@ | ||
| def max_heap(arr, n, parent_id): | ||
| largest = parent_id | ||
| left_child = 2 * largest + 1 | ||
| right_child = 2 * largest + 2 | ||
|
|
||
| if left_child < n and arr[left_child] > arr[largest]: | ||
| largest = left_child | ||
|
|
||
| if right_child < n and arr[right_child] > arr[largest]: | ||
| largest = right_child | ||
|
|
||
| if largest != parent_id: | ||
| arr[parent_id], arr[largest] = arr[largest], arr[parent_id] | ||
| max_heap(arr, n, largest) | ||
|
|
||
|
|
||
| def heap_sort(arr): | ||
| for i in range(len(arr) // 2 - 1, -1, -1): | ||
| max_heap(arr, len(arr), i) | ||
|
|
||
| end = len(arr) | ||
| for i in range(end - 1, -1, -1): | ||
| arr[0], arr[i] = arr[i], arr[0] | ||
| max_heap(arr, i, 0) | ||
|
|
||
|
|
||
| arr = [4, 10, 3, 5, 1, 10, 4, 23, 2, 3, 2, 24, -1, -51, -1, 25] | ||
|
|
||
| print(arr) | ||
| heap_sort(arr) | ||
| print(arr) | ||
|
|
||
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,74 @@ | ||
| import random | ||
| import pytest | ||
| from heap_sort import heap_sort | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def random_arr(): | ||
| random_size = random.randint(0, 1000) | ||
|
|
||
| arr = [random.randint(-1000, 1000) for _ in range(random_size)] | ||
|
|
||
| return arr | ||
|
|
||
|
|
||
| def test_empty_array(): | ||
| arr = [] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [] | ||
|
|
||
|
|
||
| def test_single_element(): | ||
| arr = [42] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [42] | ||
|
|
||
|
|
||
| def test_sorted_array(): | ||
| arr = [1, 2, 3, 4, 5] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [1, 2, 3, 4, 5] | ||
|
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. Почему бы не написать |
||
|
|
||
|
|
||
| def test_reverse_sorted_array(): | ||
| arr = [5, 4, 3, 2, 1] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [1, 2, 3, 4, 5] | ||
|
|
||
|
|
||
| def test_duplicate_elements(): | ||
| arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [1, 1, 2, 3, 3, 4, 5, 5, 6, 9] | ||
|
|
||
|
|
||
| def test_negative_numbers(): | ||
| arr = [5, -2, 0, -8, 3, 1, -1] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [-8, -2, -1, 0, 1, 3, 5] | ||
|
|
||
|
|
||
| def test_all_identical(): | ||
| arr = [7, 7, 7, 7, 7] | ||
| heap_sort(arr) | ||
|
|
||
| assert arr == [7, 7, 7, 7, 7] | ||
|
|
||
|
|
||
| def test_random_with_build_in_sort(random_arr): | ||
| random_arr2 = random_arr.copy() | ||
|
|
||
| heap_sort(random_arr) | ||
| random_arr2.sort() | ||
|
|
||
| assert random_arr == random_arr2 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| pytest.main() | ||
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.
Не знаю, учили вас этому или нет, но на питоне принято делать так:
Это нужно, чтобы этот код не исполнялся, когда Вы импортируете этот файл как модуль.