11"""
22https://en.wikipedia.org/wiki/Shellsort#Pseudocode
33"""
4+ from typing import Any , Protocol , TypeVar
45
56
6- def shell_sort (collection : list [int ]) -> list [int ]:
7+ class Comparable (Protocol ):
8+ def __lt__ (self , other : Any , / ) -> bool : ...
9+
10+
11+ T = TypeVar ("T" , bound = Comparable )
12+
13+ def shell_sort [T : Comparable ](collection : list [T ]) -> list [T ]:
714 """Pure implementation of shell sort algorithm in Python
815 :param collection: Some mutable ordered collection with heterogeneous
916 comparable items inside
@@ -15,9 +22,16 @@ def shell_sort(collection: list[int]) -> list[int]:
1522 []
1623 >>> shell_sort([-2, -5, -45])
1724 [-45, -5, -2]
25+ >>> shell_sort(["d", "a", "b", "e", "c"])
26+ ['a', 'b', 'c', 'd', 'e']
27+ >>> shell_sort([3, 1.5, 2, 0.5])
28+ [0.5, 1.5, 2, 3]
29+ >>> shell_sort([1, "two", 3])
30+ Traceback (most recent call last):
31+ ...
32+ TypeError: '>' not supported between instances of 'int' and 'str'
1833 """
19- # Marcin Ciura's gap sequence
20-
34+ # Marcin Ciura's gap sequence
2135 gaps = [701 , 301 , 132 , 57 , 23 , 10 , 4 , 1 ]
2236 for gap in gaps :
2337 for i in range (gap , len (collection )):
0 commit comments