Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e81d624
Add heapsort.py
DolzhenkoAlexa Dec 25, 2025
f48c20b
Create other_sorts.py
DolzhenkoAlexa Dec 25, 2025
98cf140
Add heapsort_tests.py
DolzhenkoAlexa Dec 25, 2025
1f0be3a
Add heapsort-tests.yml
DolzhenkoAlexa Dec 25, 2025
238f4cc
Fixed heapsort-tests.yml
DolzhenkoAlexa Dec 25, 2025
a5d5670
Create hw_heapsort
DolzhenkoAlexa Dec 26, 2025
df5390b
Delete src/hw_heapsort
DolzhenkoAlexa Dec 26, 2025
aa77001
Add heapsort.py in new dir
DolzhenkoAlexa Dec 26, 2025
a1920b6
Create other_sorts.py in new dir
DolzhenkoAlexa Dec 26, 2025
022c38a
Create tests_heapsort.py in dir only for tests
DolzhenkoAlexa Dec 26, 2025
af6339c
Delete src/hw_pytest directory
DolzhenkoAlexa Dec 26, 2025
f84f34b
Add __init__.py
DolzhenkoAlexa Dec 26, 2025
343bce3
Update heapsort-tests.yml
DolzhenkoAlexa Dec 26, 2025
0e6c02b
Create __init__.py
DolzhenkoAlexa Dec 26, 2025
8585c9f
Moved all sorts to sorts.py and rename heapsort.py to sorts.py
DolzhenkoAlexa Dec 26, 2025
e40cc76
Delete src/hw_heapsort/other_sorts.py
DolzhenkoAlexa Dec 26, 2025
e15fc7a
Update path in tests_heapsort.py
DolzhenkoAlexa Dec 26, 2025
e7db446
Delete src/hw_heapsort/__init__.py
DolzhenkoAlexa Dec 26, 2025
ec7a604
Update path in heapsort-tests.yml
DolzhenkoAlexa Dec 26, 2025
f436bb5
Update path in heapsort-tests.yml
DolzhenkoAlexa Dec 26, 2025
de8eda4
Update path in heapsort-tests.yml
DolzhenkoAlexa Dec 26, 2025
ed1e3c9
Add tests_heapsort.py out of src
DolzhenkoAlexa Dec 26, 2025
9f420f9
Add __init__.py
DolzhenkoAlexa Dec 26, 2025
f269274
Delete src/hw_heapsort/tests directory
DolzhenkoAlexa Dec 26, 2025
ba52bf6
Update path in heapsort-tests.yml
DolzhenkoAlexa Dec 26, 2025
65ded4c
Update path in heapsort-tests.yml
DolzhenkoAlexa Dec 26, 2025
e6f020d
Update path in tests_heapsort.py
DolzhenkoAlexa Dec 26, 2025
af9b2f8
Add __init__.py to create package
DolzhenkoAlexa Dec 26, 2025
acacf92
Add __init__.py to create a package
DolzhenkoAlexa Dec 26, 2025
001673d
Update path in tests_heapsort.py
DolzhenkoAlexa Dec 26, 2025
ef1ca4f
Update path in tests_heapsort.py
DolzhenkoAlexa Dec 26, 2025
fc02947
Update path in tests_heapsort.py
DolzhenkoAlexa Dec 26, 2025
f017b7e
Update path in tests_heapsort.py
DolzhenkoAlexa Dec 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/heapsort-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Heap Sort Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.13'

- name: Install pytest
run: pip install pytest

- name: Run tests
run: |
python -m pytest tests/tests_heapsort.py -v

1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/hw_heapsort/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

52 changes: 52 additions & 0 deletions src/hw_heapsort/sorts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
def heap_sort(arr):
if len(arr) <= 1:
return arr

n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)

for i in range(n - 1, 0, -1):
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0)

return arr


def heapify(arr, n, i):
while True:
largest = i
left = 2 * i + 1
right = 2 * i + 2

if left < n and arr[left] > arr[largest]:
largest = left

if right < n and arr[right] > arr[largest]:
largest = right

if largest == i:
break

arr[i], arr[largest] = arr[largest], arr[i]
i = largest


def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr


def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

136 changes: 136 additions & 0 deletions tests/tests_heapsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import pytest
from src.hw_heapsort.sorts import heap_sort, bubble_sort, insertion_sort

# обычные unit тесты и крайние случаи
@pytest.mark.empty
def test_heap_sort_empty():
"""Тест для пустого списка"""
result = heap_sort([])
assert result == []


@pytest.mark.single
def test_heap_sort_single():
"""Тест для списка из одного элемента"""
result = heap_sort([42])
assert result == [42]


@pytest.mark.sorted
def test_heap_sort_sorted():
"""Тест для уже отсортированного списка"""
result = heap_sort([1, 2, 3, 4, 5])
assert result == [1, 2, 3, 4, 5]


@pytest.mark.reversed
def test_heap_sort_reversed():
"""Тест для списка, отсортированного в обратном порядке"""
result = heap_sort([5, 4, 3, 2, 1])
assert result == [1, 2, 3, 4, 5]


@pytest.mark.duplicates
def test_heap_sort_with_duplicates():
"""Тест для списка с дубликатами"""
result = heap_sort([1, 3, 1, 3, 2, 1, 1, 1, 2])
assert result == [1, 1, 1, 1, 1, 2, 2, 3, 3]

@pytest.mark.negative
def test_heap_sort_negative_num():
"""Тест с отрицательными числами"""
result = heap_sort([-5, -1, -3, -2, -181818, -4])
assert result == [-181818, -5, -4, -3, -2, -1]


@pytest.mark.mixed
def test_heap_sort_mixed():
"""Тест с положительными и отрицательными числами"""
result = heap_sort([250000, -3, 5, -1, 2, -1000, 0, -2])
assert result == [-1000, -3, -2, -1, 0, 2, 5, 250000]


@pytest.mark.float
def test_heap_sort_floats():
"""Тест с числами с плавающей точкой"""
result = heap_sort([3.6, 1.2, 4.8, 2.1, 0.5, 2.0])
assert result == [0.5, 1.2, 2.0, 2.1, 3.6, 4.8]

# тесты для сравнения heapsort и insertion sort и bubble sort
@pytest.mark.all_sorts_empty
def test_all_sorts_empty():
"""Тест для пустого списка"""
heap_result = heap_sort([])
insertion_result = insertion_sort([])
bubble_result = bubble_sort([])
python_result = sorted([])
assert heap_result == insertion_result == bubble_result == python_result


@pytest.mark.all_sorts_single
def test_all_sorts_single():
"""Тест для списка из одного элемента"""
heap_result = heap_sort([52])
insertion_result = insertion_sort([52])
bubble_result = bubble_sort([52])
python_result = sorted([52])
assert heap_result == insertion_result == bubble_result == python_result


@pytest.mark.all_sorts_sorted
def test_all_sorts_sorted():
"""Тест для уже отсортированного списка"""
heap_result = heap_sort([1,2,3])
insertion_result = insertion_sort([1,2,3])
bubble_result = bubble_sort([1,2,3])
python_result = sorted([1,2,3])
assert heap_result == insertion_result == bubble_result == python_result


@pytest.mark.all_sorts_reversed
def test_all_sorts_reversed():
"""Тест для списка, отсортированного в обратном порядке"""
heap_result = heap_sort([5, 4, 3])
insertion_result = insertion_sort([5, 4, 3])
bubble_result = bubble_sort([5, 4, 3])
python_result = sorted([5, 4, 3])
assert heap_result == insertion_result == bubble_result == python_result


@pytest.mark.all_sorts_duplicates
def test_all_sorts_with_duplicates():
"""Тест для списка с дубликатами"""
heap_result = heap_sort([0, 0, 3, 5, 4, 5, 5, 3, 0, 0])
insertion_result = insertion_sort([0, 0, 3, 5, 4, 5, 5, 3, 0, 0])
bubble_result = bubble_sort([0, 0, 3, 5, 4, 5, 5, 3, 0, 0])
python_result = sorted([0, 0, 3, 5, 4, 5, 5, 3, 0, 0])
assert heap_result == insertion_result == bubble_result == python_result

@pytest.mark.all_sorts_negative
def test_all_sorts_negative_num():
"""Тест с отрицательными числами"""
heap_result = heap_sort([-762, -10, -387, -37072])
insertion_result = insertion_sort([-762, -10, -387, -37072])
bubble_result = bubble_sort([-762, -10, -387, -37072])
python_result = sorted([-762, -10, -387, -37072])
assert heap_result == insertion_result == bubble_result == python_result


@pytest.mark.all_sorts_mixed
def test_all_sorts_mixed():
"""Тест с положительными и отрицательными числами"""
heap_result = heap_sort([100, -1, 0, -387, 153])
insertion_result = insertion_sort([100, -1, 0, -387, 153])
bubble_result = bubble_sort([100, -1, 0, -387, 153])
python_result = sorted([100, -1, 0, -387, 153])
assert heap_result == insertion_result == bubble_result == python_result


@pytest.mark.all_sorts_float
def test_all_sorts_floats():
"""Тест с числами с плавающей точкой"""
heap_result = heap_sort([1.0, -1.9, 0.0, -3.8, 153.5])
insertion_result = insertion_sort([1.0, -1.9, 0.0, -3.8, 153.5])
bubble_result = bubble_sort([1.0, -1.9, 0.0, -3.8, 153.5])
python_result = sorted([1.0, -1.9, 0.0, -3.8, 153.5])
assert heap_result == insertion_result == bubble_result == python_result