-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinsertion_sort.py
37 lines (32 loc) · 957 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
"""
https://en.wikipedia.org/wiki/Insertion_sort
"""
def insertion_sort(array):
"""
Bubble sort algorithm.
:param array: the array to be sorted.
:return: sorted array.
>>> import random
>>> array = random.sample(range(-50, 50), 100)
>>> insertion_sort(array) == sorted(array)
True
>>> import string
>>> array = random.choices(string.ascii_letters + string.digits, k = 100)
>>> insertion_sort(array) == sorted(array)
True
>>> array = [random.uniform(-50.0, 50.0) for i in range(100)]
>>> insertion_sort(array) == sorted(array)
True
"""
for i in range(1, len(array)):
j = i - 1
key = array[i]
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j -= 1
if j != i - 1:
array[j + 1] = key
return array
if __name__ == "__main__":
from doctest import testmod
testmod()