diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml new file mode 100644 index 0000000..0695e93 --- /dev/null +++ b/.github/workflows/run_tests.yml @@ -0,0 +1,29 @@ +name: Python application + +on: [push] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.13 + uses: actions/setup-python@v3 + with: + python-version: "3.13" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install ruff + pip install pytest + - name: Lint with ruff + run: | + ruff check --output-format=github + - name: Test with pytest + run: | + pytest \ No newline at end of file diff --git a/heap_sort.py b/heap_sort.py new file mode 100644 index 0000000..47e4c3c --- /dev/null +++ b/heap_sort.py @@ -0,0 +1,42 @@ +# функция для постройки максимальной кучи +def built_heap(array, lenght, ind): + + # создание корня и потомков + large = ind + left = ind * 2 + 1 + right = ind * 2 +2 + + # сравнивание корня с потомками и проверка существования потомков + if left < lenght and array[large] < array[left]: + large = left + + if right < lenght and array[large] < array[right]: + large = right + + # если наибольший элемент не корень, меняем значения местами и делаем рекурсию + if large != ind: + array[ind], array[large] = array[large], array[ind] + built_heap(array, lenght, large) + + +def heap_sort(array): + lenght = len(array) + + # построение максимальной кучи, начиная с последнего узла + for ind in range(lenght // 2 - 1, -1, -1): + built_heap(array, lenght, ind) + + # извлечение элементов из кучи + for ind in range(lenght - 1, 0, -1): + + # перемещаем корень в конец, т.к. это макс элемент + array[ind], array[0] = array[0], array[ind] + + # создаем уже уменьшанную кучу + built_heap(array, ind, 0) + + return array + + + + diff --git a/test_heap_sort.py b/test_heap_sort.py new file mode 100644 index 0000000..ed0d48c --- /dev/null +++ b/test_heap_sort.py @@ -0,0 +1,25 @@ +from heap_sort import heap_sort + +# unit тесты +def test_unit_sort_1(): + assert heap_sort([1,32253,3,533,5,0]) == [0, 1, 3, 5, 533, 32253] + +def test_unit_sort_2(): + assert heap_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5, 6, 7, 8, 9] + +# критические случаи +def test_crit_case_sort_1(): + assert heap_sort([1, 1, 1, 1, 1, 1]) == [1, 1, 1, 1, 1, 1] + +def test_crit_case_sort_2(): + assert heap_sort([0, 1, 3, 5, 533, 32253]) == [0, 1, 3, 5, 533, 32253] + +def test_crit_case_sort_3(): + assert heap_sort([1]) == [1] + +def test_crit_case_sort_4(): + assert heap_sort([]) == [] + +# property based тест с другой реализованной сортировкой +def test_prop_based_sort(): + assert sorted([1,32253,3,533,5,0]) == heap_sort([1,32253,3,533,5,0])