Skip to content

Commit cc84768

Browse files
committed
Add Shell Sort algorithm implementation
1 parent ae68a78 commit cc84768

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

sorts/shell_sort.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
https://en.wikipedia.org/wiki/Shellsort#Pseudocode
33
"""
44

5-
65
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
6+
"""
7+
Sort a list of integers using the Shell Sort algorithm.
8+
9+
Reference:
10+
https://en.wikipedia.org/wiki/Shellsort
1111
1212
>>> shell_sort([0, 5, 3, 2, 2])
1313
[0, 2, 2, 3, 5]
@@ -16,25 +16,15 @@ def shell_sort(collection: list[int]) -> list[int]:
1616
>>> shell_sort([-2, -5, -45])
1717
[-45, -5, -2]
1818
"""
19-
# Marcin Ciura's gap sequence
20-
2119
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
20+
2221
for gap in gaps:
2322
for i in range(gap, len(collection)):
2423
insert_value = collection[i]
2524
j = i
2625
while j >= gap and collection[j - gap] > insert_value:
2726
collection[j] = collection[j - gap]
2827
j -= gap
29-
if j != i:
30-
collection[j] = insert_value
31-
return collection
32-
33-
34-
if __name__ == "__main__":
35-
from doctest import testmod
28+
collection[j] = insert_value
3629

37-
testmod()
38-
user_input = input("Enter numbers separated by a comma:\n").strip()
39-
unsorted = [int(item) for item in user_input.split(",")]
40-
print(shell_sort(unsorted))
30+
return collection

0 commit comments

Comments
 (0)