Skip to content

Commit 4dacca5

Browse files
committed
Better
1 parent c3d4b9e commit 4dacca5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

sorts/merge_sort.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,19 @@ def merge(left: list, right: list) -> list:
3737
:return: Merged result
3838
"""
3939
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)
40+
left_index = right_index = 0
41+
length_left , length_right = len(left) , len(right)
42+
43+
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: ])
4453
return result
4554

4655
if len(collection) <= 1:

0 commit comments

Comments
 (0)