-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.py
30 lines (22 loc) · 924 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
from typing import List
from ..decorators import process_timer
@process_timer
def insertion_sort(array: List[int]) -> List[int]:
"""
## Insertion Sort
Insertion sort is used when number of elements is small.
It can also be useful when input array is almost sorted,
only few elements are misplaced in complete big array.
Time Complexity: `O(N^2)`, If items are reverse order, worst case complexity occurs.
Auxiliary Space: `O(1)`, insertion sort is an in-place sorting algorithm.
:param array: list of integer numbers that we want sort
:type array: list[int]
:return: list of sorted integer number with insertion sort algorithm
:rtype: list[int]
"""
for index, item in enumerate(array[1:], start=1):
while 0 < index and item < array[index - 1]:
array[index] = array[index - 1]
index -= 1
array[index] = item
return array