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
18 changes: 17 additions & 1 deletion sorts/shell_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
https://en.wikipedia.org/wiki/Shellsort#Pseudocode
"""

from collections.abc import MutableSequence
from typing import Any, Protocol

def shell_sort(collection: list[int]) -> list[int]:

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


def shell_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]:
"""Pure implementation of shell sort algorithm in Python
:param collection: Some mutable ordered collection with heterogeneous
comparable items inside
Expand All @@ -15,6 +22,15 @@ def shell_sort(collection: list[int]) -> list[int]:
[]
>>> shell_sort([-2, -5, -45])
[-45, -5, -2]
>>> shell_sort(["c", "a", "b"])
['a', 'b', 'c']
>>> shell_sort([2.5, -1, 0.0])
[-1, 0.0, 2.5]
>>> shell_sort(["c", "a", "b"]) == sorted(["c", "a", "b"])
True
>>> import pytest
>>> with pytest.raises(TypeError):
... shell_sort([1, "a"])
"""
# Marcin Ciura's gap sequence

Expand Down
1 change: 1 addition & 0 deletions tests/test_sorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def test_sort_matches_builtin(sort, case):
insertion_sort,
merge_sort,
selection_sort,
shell_sort,
],
ids=lambda f: f.__name__,
)
Expand Down
Loading