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
3 changes: 0 additions & 3 deletions HW2-PersonalPage/_config.yml

This file was deleted.

10 changes: 0 additions & 10 deletions HW2-PersonalPage/index.md

This file was deleted.

28 changes: 28 additions & 0 deletions HW5/heapSort/src/heap_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def heapify(arr, size, ind):
largest = ind
l_ind = 2 * ind + 1
r_ind = 2 * ind + 2

if l_ind < size and arr[l_ind] > arr[largest]:
largest = l_ind

if r_ind < size and arr[r_ind] > arr[largest]:
largest = r_ind

if largest != ind:
arr[ind], arr[largest] = arr[largest], arr[ind]
heapify(arr, size, largest)


def heap_sort(arr):
size = len(arr)

for i in range(size // 2 - 1, -1, -1):
heapify(arr, size, i)

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

return arr

28 changes: 28 additions & 0 deletions HW5/heapSort/src/sorts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def python_sorted(arr):
return sorted(arr)


def set_pivot(arr, low, high):
pivot = arr[high]
i = low - 1

for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]

arr[high], arr[i + 1] = arr[i + 1], arr[high]
return i + 1


def quick_sort(arr):
if len(arr) <= 1:
return arr

pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]

return quick_sort(left) + middle + quick_sort(right)

34 changes: 34 additions & 0 deletions HW5/heapSort/test/test_heapsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from src.heap_sort import heap_sort


class TestHeapSort:
def test_empty_array(self):
assert heap_sort([]) == []

def test_single_element(self):
assert heap_sort([5]) == [5]

def test_two_elements_unsorted(self):
assert heap_sort([2, 1]) == [1, 2]

def test_positive_numbers(self):
assert heap_sort([4, 2, 8, 1, 3]) == [1, 2, 3, 4, 8]

def test_negative_numbers(self):
assert heap_sort([-3, -1, -2, -5]) == [-5, -3, -2, -1]

def test_mixed_numbers(self):
assert heap_sort([3, -2, 0, -1, 5]) == [-2, -1, 0, 3, 5]

def test_duplicate_values(self):
assert heap_sort([3, 1, 3, 2, 1]) == [1, 1, 2, 3, 3]

def test_already_sorted(self):
assert heap_sort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]

def test_reverse_sorted(self):
assert heap_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5]

def test_identical_elements(self):
assert heap_sort([7, 7, 7, 7]) == [7, 7, 7, 7]

39 changes: 39 additions & 0 deletions HW5/heapSort/test/test_heapsort_properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from hypothesis import given, strategies as strat
from collections import Counter
from src.heap_sort import heap_sort
from src.sorts import python_sorted, quick_sort

SORTING_ALGORITHMS = [heap_sort, python_sorted, quick_sort]


class TestHeapSortProperties:
@given(strat.lists(strat.integers(), max_size=50))
def test_heapsort_equals_other_sorts(self, arr):
heapsort_result = heap_sort(arr.copy())
for other_sort in [quick_sort, python_sorted]:
other_result = other_sort(arr.copy())
assert heapsort_result == other_result

@given(strat.lists(strat.integers()))
def test_heapsort_preserves_length(self, arr):
result = heap_sort(arr.copy())
assert len(result) == len(arr)

@given(strat.lists(strat.integers()))
def test_heapsort_produces_sorted_output(self, arr):
result = heap_sort(arr.copy())
for i in range(len(result) - 1):
assert result[i] <= result[i + 1]

@given(strat.lists(strat.integers()))
def test_heapsort_preserves_elements(self, arr):
result = heap_sort(arr.copy())
assert Counter(result) == Counter(arr)

@given(strat.lists(strat.integers(), min_size=2, max_size=20))
def test_heapsort_with_duplicates(self, arr):
test_arr = arr + arr.copy()
heapsort_result = heap_sort(test_arr.copy())
python_result = python_sorted(test_arr.copy())
assert heapsort_result == python_result