From 1276f75b849f1bd847390eee24a975260c3b9b75 Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Fri, 17 Oct 2025 22:33:14 +0300 Subject: [PATCH 1/2] Add heapsort and tests --- src/heapsort/heapsort.py | 26 +++++++++++++++++++++++++ src/heapsort/tests.py | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/heapsort/heapsort.py create mode 100644 src/heapsort/tests.py diff --git a/src/heapsort/heapsort.py b/src/heapsort/heapsort.py new file mode 100644 index 0000000..ae863b7 --- /dev/null +++ b/src/heapsort/heapsort.py @@ -0,0 +1,26 @@ +def heapify(nums, heap_size, root_index): + largest = root_index + left_child = (2 * root_index) + 1 + right_child = (2 * root_index) + 2 + + + if left_child < heap_size and nums[left_child] > nums[largest]: + largest = left_child + + if right_child < heap_size and nums[right_child] > nums[largest]: + largest = right_child + + if largest != root_index: + nums[root_index], nums[largest] = nums[largest], nums[root_index] + heapify(nums, heap_size, largest) + +def heap_sort(nums): + n = len(nums) + + for i in range(n, -1, -1): + heapify(nums, n, i) + + for i in range(n - 1, 0, -1): + nums[i], nums[0] = nums[0], nums[i] + heapify(nums, i, 0) + return nums diff --git a/src/heapsort/tests.py b/src/heapsort/tests.py new file mode 100644 index 0000000..825f23d --- /dev/null +++ b/src/heapsort/tests.py @@ -0,0 +1,42 @@ +from heapsort import heap_sort +import pytest +import random + + +@pytest.fixture +def random_list(): + rand_list = random.randint(1, 100) + random_delta = random.randint(0, 10000) + rand_list = list(range(random_delta, rand_list + random_delta)) + random.shuffle(rand_list) + return rand_list + +@pytest.mark.parametrize( + ["n", "expected"], + [([1], [1]), + ([2, 7, 4, 8, 6, 5], [2, 4, 5, 6, 7, 8]), + ([], []), + ([9, 8, 7, 6, 5, 4, 3, 2, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9]), + ([1, 3, 2, 3, 1], [1, 1, 2, 3, 3])] +) +def test_heapsort_main(n, expected): + assert heap_sort(n) == expected + +def test_heapsort_random(random_list): + assert heap_sort(random_list) == list(sorted(random_list)) + +rand_tests = [] +for i in range(10): + rand_list = random.randint(1, 100) + random_delta = random.randint(0, 10000) + rand_list = list(range(random_delta, rand_list + random_delta)) + random.shuffle(rand_list) + tup = (rand_list, sorted(rand_list)) + rand_tests.append(tup) + +@pytest.mark.parametrize( + ["n", "expected"], + rand_tests +) +def test_heapsort_random_multiple(n, expected): + assert heap_sort(n) == expected From e356195794eff975eabd8f0e3f45274392c5e15d Mon Sep 17 00:00:00 2001 From: Mikhail Romanovskiy Date: Sat, 18 Oct 2025 00:14:55 +0300 Subject: [PATCH 2/2] Fix pytest --- .github/workflows/pytest.yml | 4 ++-- src/heapsort/{tests.py => heapsort_test.py} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename src/heapsort/{tests.py => heapsort_test.py} (100%) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 593b5ee..a39af27 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -16,5 +16,5 @@ jobs: pip install -r requirements.txt - name: Test with pytest run: | - pip install pytest pytest-cov - pytest tests.py --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html + pip install pytest + pytest diff --git a/src/heapsort/tests.py b/src/heapsort/heapsort_test.py similarity index 100% rename from src/heapsort/tests.py rename to src/heapsort/heapsort_test.py