Skip to content
Closed
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
22 changes: 20 additions & 2 deletions 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 Any, Protocol

def merge_sort(collection: list) -> list:

class Comparable(Protocol):
def __lt__(self, other: Any, /) -> 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,8 +28,20 @@ def merge_sort(collection: list) -> list:

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

>>> merge_sort(["banana", "apple", "cherry"])
['apple', 'banana', 'cherry']

>>> merge_sort([3.14, 1.5, 2.7])
[1.5, 2.7, 3.14]

>>> merge_sort([1, "a"]) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: ...
"""
start, end = [], []
start: list[T] = []
end: list[T] = []
while len(collection) > 1:
min_one, max_one = min(collection), max(collection)
start.append(min_one)
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_merge_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_merge_sort,
)


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