Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions heap_sort.py
Original file line number Diff line number Diff line change
@@ -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




25 changes: 25 additions & 0 deletions test_heap_sort.py
Original file line number Diff line number Diff line change
@@ -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])