Skip to content

Commit 48b9a8c

Browse files
Refine Shell Sort implementation and documentation
1 parent 791deb4 commit 48b9a8c

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

sorts/shell_sort.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
"""
1+
"""Shell sort implementation.
2+
3+
Reference:
24
https://en.wikipedia.org/wiki/Shellsort#Pseudocode
35
"""
46

57

68
def shell_sort(collection: list[int]) -> list[int]:
7-
"""Pure implementation of shell sort algorithm in Python
8-
:param collection: Some mutable ordered collection with heterogeneous
9-
comparable items inside
10-
:return: the same collection ordered by ascending
9+
"""Sort a list of integers in ascending order using shell sort.
10+
11+
:param collection: A list of integers
12+
:return: The same list sorted in ascending order
1113
1214
>>> shell_sort([0, 5, 3, 2, 2])
1315
[0, 2, 2, 3, 5]
@@ -16,18 +18,19 @@ def shell_sort(collection: list[int]) -> list[int]:
1618
>>> shell_sort([-2, -5, -45])
1719
[-45, -5, -2]
1820
"""
19-
# Marcin Ciura's gap sequence
20-
2121
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
22+
2223
for gap in gaps:
2324
for i in range(gap, len(collection)):
2425
insert_value = collection[i]
2526
j = i
27+
2628
while j >= gap and collection[j - gap] > insert_value:
2729
collection[j] = collection[j - gap]
2830
j -= gap
29-
if j != i:
30-
collection[j] = insert_value
31+
32+
collection[j] = insert_value
33+
3134
return collection
3235

3336

@@ -37,4 +40,4 @@ def shell_sort(collection: list[int]) -> list[int]:
3740
testmod()
3841
user_input = input("Enter numbers separated by a comma:\n").strip()
3942
unsorted = [int(item) for item in user_input.split(",")]
40-
print(shell_sort(unsorted))
43+
print(shell_sort(unsorted))

0 commit comments

Comments
 (0)