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
16 changes: 15 additions & 1 deletion sorts/shrink_shell_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
using a smaller gap, the list is sorted more quickly.
"""

from typing import Protocol

def shell_sort(collection: list) -> list:

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


def shell_sort[T: Comparable](collection: list[T]) -> list[T]:
"""Implementation of shell sort algorithm in Python
:param collection: Some mutable ordered collection with heterogeneous
comparable items inside
Expand All @@ -33,6 +39,14 @@ def shell_sort(collection: list) -> list:
[]
>>> shell_sort([1])
[1]
>>> shell_sort(["pear", "apple", "orange"])
['apple', 'orange', 'pear']
>>> shell_sort([2.5, -1, 0.0])
[-1, 0.0, 2.5]
>>> shell_sort([1, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: ...
"""

# Choose an initial gap value
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from sorts.quick_sort import quick_sort
from sorts.selection_sort import selection_sort
from sorts.shell_sort import shell_sort
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

Expand Down Expand Up @@ -68,6 +69,7 @@ def test_heap_sort() -> None:
quick_sort,
selection_sort,
shell_sort,
shrink_shell_sort,
stooge_sort,
strand_sort,
)
Expand Down Expand Up @@ -122,6 +124,7 @@ def test_sort_matches_builtin(sort, case) -> None:
insertion_sort,
merge_sort,
selection_sort,
shrink_shell_sort,
],
ids=lambda f: f.__name__,
)
Expand Down
Loading