Skip to content

Commit a85209f

Browse files
Refactor adaptive_merge_sort for type safety with Protocol (#15367)
* Refactor adaptive_merge_sort for type safety with Protocol * Add doctests for adaptive_merge_sort Removed print statements for initial sequence, sorted sequence, sorting steps, and after merge. * Fix quotes in adaptive_merge_sort docstring examples Updated docstring examples to use consistent quotes. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove doctests incompatible with parallel test runner Removed docstring examples from adaptive_merge_sort function. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix whitespace in adaptive_merge_sort --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 92fa11b commit a85209f

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

‎sorts/adaptive_merge_sort.py‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
1-
def adaptive_merge_sort(sequence: list) -> list:
1+
from typing import Protocol
2+
3+
4+
class Comparable(Protocol):
5+
def __lt__(self, other: object, /) -> bool: ...
6+
7+
8+
def adaptive_merge_sort[T: Comparable](sequence: list[T]) -> list[T]:
29
if len(sequence) < 2:
310
return sequence
411

512
aux = sequence[:]
6-
print(f"Initial sequence: {sequence}")
713
adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1)
8-
print(f"Sorted sequence: {sequence}")
914
return sequence
1015

1116

12-
def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None:
17+
def adaptive_merge_sort_helper[T: Comparable](
18+
array: list[T], aux: list[T], low: int, high: int
19+
) -> None:
1320
if high <= low:
1421
return
1522

1623
mid = (low + high) // 2
17-
print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")
1824

1925
adaptive_merge_sort_helper(aux, array, low, mid)
2026
adaptive_merge_sort_helper(aux, array, mid + 1, high)
2127

22-
if array[mid] <= array[mid + 1]:
23-
print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]")
28+
if not array[mid + 1] < array[mid]:
2429
array[low : high + 1] = aux[low : high + 1]
2530
return
2631

2732
merge(array, aux, low, mid, high)
2833

2934

30-
def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
31-
print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")
32-
35+
def merge[T: Comparable](
36+
array: list[T], aux: list[T], low: int, mid: int, high: int
37+
) -> None:
3338
i, j = low, mid + 1
39+
3440
for k in range(low, high + 1):
3541
if i > mid or j > high:
3642
if i > mid:
@@ -39,7 +45,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
3945
else:
4046
aux[k] = array[i]
4147
i += 1
42-
elif array[i] <= array[j]:
48+
elif not array[j] < array[i]:
4349
aux[k] = array[i]
4450
i += 1
4551
else:
@@ -48,10 +54,3 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
4854

4955
for k in range(low, high + 1):
5056
array[k] = aux[k]
51-
52-
print(f"After merge: {array[low : high + 1]}")
53-
54-
55-
# Example usage
56-
if __name__ == "__main__":
57-
print(adaptive_merge_sort([4, 3, 1, 2]))

0 commit comments

Comments
 (0)