We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c3d4b9e commit 4dacca5Copy full SHA for 4dacca5
1 file changed
sorts/merge_sort.py
@@ -37,10 +37,19 @@ def merge(left: list, right: list) -> list:
37
:return: Merged result
38
"""
39
result = []
40
- while left and right:
41
- result.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
42
- result.extend(left)
43
- result.extend(right)
+ left_index = right_index = 0
+ length_left , length_right = len(left) , len(right)
+
+ while (left_index < length_left) and (right_index < length_right):
44
+ if left[left_index] < right[right_index]:
45
+ result.append(left[left_index])
46
+ left_index += 1
47
+ else:
48
+ result.append(right[right_index])
49
+ right_index += 1
50
51
+ result.extend(left[left_index: ])
52
+ result.extend(right[right_index: ])
53
return result
54
55
if len(collection) <= 1:
0 commit comments