Skip to content
Merged
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
19 changes: 18 additions & 1 deletion sorts/unknown_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
already O(n)
"""

from typing import Protocol

def merge_sort(collection: list) -> list:

class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...


def merge_sort[T: Comparable](collection: list[T]) -> list[T]:
"""Pure implementation of the fastest merge sort algorithm in Python

:param collection: some mutable ordered collection with heterogeneous
Expand All @@ -22,6 +28,17 @@ def merge_sort(collection: list) -> list:

>>> merge_sort([-2, -5, -45])
[-45, -5, -2]

>>> merge_sort(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
True

>>> merge_sort([1.1, 3.3, 5.5, 2.2, 4.4])
[1.1, 2.2, 3.3, 4.4, 5.5]

>>> merge_sort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""
start, end = [], []
while len(collection) > 1:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from sorts.shrink_shell_sort import shell_sort as shrink_shell_sort
from sorts.stooge_sort import stooge_sort
from sorts.strand_sort import strand_sort
from sorts.unknown_sort import merge_sort as unknown_sort


def test_heap_sort() -> None:
Expand Down Expand Up @@ -85,6 +86,7 @@ def test_heap_sort() -> None:
shrink_shell_sort,
stooge_sort,
strand_sort,
unknown_sort,
)


Expand Down Expand Up @@ -153,6 +155,7 @@ def test_rec_insertion_sort(case) -> None:
selection_sort,
shrink_shell_sort,
strand_sort,
unknown_sort,
],
ids=lambda f: f.__name__,
)
Expand Down
Loading