Skip to content

Commit ee28dee

Browse files
grochedixcclauss
andauthored
Insertion sort : type hint, docstring (TheAlgorithms#2327)
* insertion sort : docstring, type hinting * Update insertion_sort.py Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent a46b555 commit ee28dee

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

sorts/insertion_sort.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
"""
2-
This is a pure Python implementation of the insertion sort algorithm
2+
A pure Python implementation of the insertion sort algorithm
3+
4+
This algorithm sorts a collection by comparing adjacent elements.
5+
When it finds that order is not respected, it moves the element compared
6+
backward until the order is correct. It then goes back directly to the
7+
element's initial position resuming forward comparison.
38
49
For doctests run following command:
5-
python -m doctest -v insertion_sort.py
6-
or
710
python3 -m doctest -v insertion_sort.py
811
912
For manual testing run:
10-
python insertion_sort.py
13+
python3 insertion_sort.py
1114
"""
1215

1316

14-
def insertion_sort(collection):
15-
"""Pure implementation of the insertion sort algorithm in Python
17+
def insertion_sort(collection: list) -> list:
18+
"""A pure Python implementation of the insertion sort algorithm
1619
1720
:param collection: some mutable ordered collection with heterogeneous
1821
comparable items inside
@@ -47,4 +50,4 @@ def insertion_sort(collection):
4750
if __name__ == "__main__":
4851
user_input = input("Enter numbers separated by a comma:\n").strip()
4952
unsorted = [int(item) for item in user_input.split(",")]
50-
print(insertion_sort(unsorted))
53+
print(f"{insertion_sort(unsorted) = }")

0 commit comments

Comments
 (0)