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
18 changes: 17 additions & 1 deletion sorts/strand_sort.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import operator
from typing import Protocol, TypeVar


def strand_sort(arr: list, reverse: bool = False, solution: list | None = None) -> list:
class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...

def __gt__(self, other: object, /) -> bool: ...


T = TypeVar("T", bound=Comparable)


def strand_sort[T](
arr: list[T], reverse: bool = False, solution: list[T] | None = None
) -> list[T]:
"""
Strand sort implementation
source: https://en.wikipedia.org/wiki/Strand_sort
Expand All @@ -16,6 +28,10 @@ def strand_sort(arr: list, reverse: bool = False, solution: list | None = None)

>>> strand_sort([4, 2, 5, 3, 0, 1], reverse=True)
[5, 4, 3, 2, 1, 0]

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

"""
_operator = operator.lt if reverse else operator.gt
solution = solution or []
Expand Down
1 change: 1 addition & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def test_rec_insertion_sort(case) -> None:
pancake_sort,
selection_sort,
shrink_shell_sort,
strand_sort,
],
ids=lambda f: f.__name__,
)
Expand Down
Loading