-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsertion_sort.py
37 lines (25 loc) · 940 Bytes
/
insertion_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import List
def insertion_sort(list: List[int]) -> List[int]:
"""Insertion Sort - O(n^2)
https://en.wikipedia.org/wiki/Insertion_sort
Sorting by inserting the least value in every iteration.
Args:
list (List[int]): a collection with comparable items
Returns:
List[int]: sorted collection
Tests:
>>> collection = [79, 53, 10, 85, 13, 60, 11, 69, 99, 56]
>>> print(insertion_sort(collection))
[10, 11, 13, 53, 56, 60, 69, 79, 85, 99]
"""
for i in range(1, len(list)):
current_value = list[i]
current_position = i
while current_position > 0 and list[current_position - 1] > current_value:
list[current_position] = list[current_position - 1]
current_position -= 1
list[current_position] = current_value
return list
if __name__ == '__main__':
from doctest import testmod
testmod()