Skip to content

Commit d7df6d6

Browse files
MaximSmolskiyCjkjvfnby
authored andcommitted
Reduce the complexity of sorts/merge_insertion_sort.py (TheAlgorithms#7954)
* Reduce the complexity of sorts/merge_insertion_sort.py * Add tests * Lower the --max-complexity threshold in the file .flake8
1 parent 55e98fb commit d7df6d6

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

Diff for: .flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[flake8]
22
max-line-length = 88
33
# max-complexity should be 10
4-
max-complexity = 19
4+
max-complexity = 17
55
extend-ignore =
66
# Formatting style for `black`
77
# E203 is whitespace before ':'

Diff for: sorts/merge_insertion_sort.py

+47-32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,53 @@
1414
from __future__ import annotations
1515

1616

17+
def binary_search_insertion(sorted_list, item):
18+
"""
19+
>>> binary_search_insertion([1, 2, 7, 9, 10], 4)
20+
[1, 2, 4, 7, 9, 10]
21+
"""
22+
left = 0
23+
right = len(sorted_list) - 1
24+
while left <= right:
25+
middle = (left + right) // 2
26+
if left == right:
27+
if sorted_list[middle] < item:
28+
left = middle + 1
29+
break
30+
elif sorted_list[middle] < item:
31+
left = middle + 1
32+
else:
33+
right = middle - 1
34+
sorted_list.insert(left, item)
35+
return sorted_list
36+
37+
38+
def merge(left, right):
39+
"""
40+
>>> merge([[1, 6], [9, 10]], [[2, 3], [4, 5], [7, 8]])
41+
[[1, 6], [2, 3], [4, 5], [7, 8], [9, 10]]
42+
"""
43+
result = []
44+
while left and right:
45+
if left[0][0] < right[0][0]:
46+
result.append(left.pop(0))
47+
else:
48+
result.append(right.pop(0))
49+
return result + left + right
50+
51+
52+
def sortlist_2d(list_2d):
53+
"""
54+
>>> sortlist_2d([[9, 10], [1, 6], [7, 8], [2, 3], [4, 5]])
55+
[[1, 6], [2, 3], [4, 5], [7, 8], [9, 10]]
56+
"""
57+
length = len(list_2d)
58+
if length <= 1:
59+
return list_2d
60+
middle = length // 2
61+
return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:]))
62+
63+
1764
def merge_insertion_sort(collection: list[int]) -> list[int]:
1865
"""Pure implementation of merge-insertion sort algorithm in Python
1966
@@ -38,38 +85,6 @@ def merge_insertion_sort(collection: list[int]) -> list[int]:
3885
True
3986
"""
4087

41-
def binary_search_insertion(sorted_list, item):
42-
left = 0
43-
right = len(sorted_list) - 1
44-
while left <= right:
45-
middle = (left + right) // 2
46-
if left == right:
47-
if sorted_list[middle] < item:
48-
left = middle + 1
49-
break
50-
elif sorted_list[middle] < item:
51-
left = middle + 1
52-
else:
53-
right = middle - 1
54-
sorted_list.insert(left, item)
55-
return sorted_list
56-
57-
def sortlist_2d(list_2d):
58-
def merge(left, right):
59-
result = []
60-
while left and right:
61-
if left[0][0] < right[0][0]:
62-
result.append(left.pop(0))
63-
else:
64-
result.append(right.pop(0))
65-
return result + left + right
66-
67-
length = len(list_2d)
68-
if length <= 1:
69-
return list_2d
70-
middle = length // 2
71-
return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:]))
72-
7388
if len(collection) <= 1:
7489
return collection
7590

0 commit comments

Comments
 (0)