-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
59 lines (47 loc) · 1.76 KB
/
insertion_sort.py
File metadata and controls
59 lines (47 loc) · 1.76 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import unittest
import random
def insertion_sort(arr: list[int]) -> list[int]:
for i, el in enumerate(arr[1:], 1):
# for j in range(i - 1, -1, -1):
j = i - 1
while j >= 0:
if arr[j] > el:
arr[j + 1] = arr[j]
else:
break
j -= 1
arr[j + 1] = el
# ]
return arr
class InsertionTest(unittest.TestCase):
def test_sort_1(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
def test_sort_2(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
def test_sort_3(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
def test_sort_4(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
def test_sort_5(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
def test_sort_6(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
def test_sort_7(self):
arr = list(range(10, 30))
print(insertion_sort(arr))
self.assertEqual(insertion_sort(arr), sorted(arr))
if __name__ == "__main__":
# print(insertion_sort([1, 2, 3, 4, 9, 5, 6, 8, 10]))
unittest.main()