1- """Shell sort implementation.
1+ """
2+ Shell sort implementation.
23
34Reference:
45https://en.wikipedia.org/wiki/Shellsort#Pseudocode
56"""
67
78
89def shell_sort (collection : list [int ]) -> list [int ]:
9- """Sort a list of integers in ascending order using shell sort.
10+ """
11+ Sort a list of integers in ascending order using Shell sort.
1012
11- :param collection: A list of integers
13+ :param collection: A list of integers to sort
1214 :return: The same list sorted in ascending order
1315
1416 >>> shell_sort([0, 5, 3, 2, 2])
@@ -17,18 +19,24 @@ def shell_sort(collection: list[int]) -> list[int]:
1719 []
1820 >>> shell_sort([-2, -5, -45])
1921 [-45, -5, -2]
22+ >>> shell_sort([3, "a"]) # doctest: +IGNORE_EXCEPTION_DETAIL
23+ Traceback (most recent call last):
24+ TypeError
2025 """
2126 gaps = [701 , 301 , 132 , 57 , 23 , 10 , 4 , 1 ]
2227
2328 for gap in gaps :
24- for i in range (gap , len (collection )):
25- insert_value = collection [i ]
26- j = i
27-
28- while j >= gap and collection [j - gap ] > insert_value :
29- collection [j ] = collection [j - gap ]
30- j -= gap
31-
32- collection [j ] = insert_value
29+ for index in range (gap , len (collection )):
30+ insert_value = collection [index ]
31+ current_index = index
32+
33+ while (
34+ current_index >= gap
35+ and collection [current_index - gap ] > insert_value
36+ ):
37+ collection [current_index ] = collection [current_index - gap ]
38+ current_index -= gap
39+
40+ collection [current_index ] = insert_value
3341
3442 return collection
0 commit comments