-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.py
29 lines (24 loc) · 958 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
# coding: utf-8
"""
|------------------------------------------------------------|
| Insertion Sort |
|------------------------------------------------------------|
|Type || In-place stable sort |
|Worst case performance || O(n2) comparisons, swaps |
|Best case performance || Ω(n) comparisons, O(1) swaps|
|Average case performance || O(n2) comparisons, swaps |
|Worst case space complexity || O(n) total, O(1) auxiliary |
|------------------------------------------------------------|
"""
from decorators import timethis
@timethis
def insertion_sort(array):
size = len(array)
for index in range(1, size):
val = array[index]
position = index
while position > 0 and array[position-1] > val:
array[position] = array[position-1]
position = position-1
array[position] = val
return array