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

from typing import Any, Protocol, TypeVar

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

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


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


def shell_sort[T: Comparable](collection: list[T]) -> list[T]:
"""Pure implementation of shell sort algorithm in Python
:param collection: Some mutable ordered collection with heterogeneous
comparable items inside
Expand All @@ -15,9 +24,16 @@ def shell_sort(collection: list[int]) -> list[int]:
[]
>>> shell_sort([-2, -5, -45])
[-45, -5, -2]
>>> shell_sort(["d", "a", "b", "e", "c"])
['a', 'b', 'c', 'd', 'e']
>>> shell_sort([3, 1.5, 2, 0.5])
[0.5, 1.5, 2, 3]
>>> shell_sort([1, "two", 3])
Traceback (most recent call last):
...
TypeError: '>' not supported between instances of 'int' and 'str'
"""
# Marcin Ciura's gap sequence

gaps = [701, 301, 132, 57, 23, 10, 4, 1]
for gap in gaps:
for i in range(gap, len(collection)):
Expand Down
Loading