Skip to content
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions sorts/merge_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ def merge(left: list, right: list) -> list:
:return: Merged result
"""
result = []
while left and right:
result.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
result.extend(left)
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):
if left[left_index] < right[right_index]:
result.append(left[left_index])
left_index += 1
else:
result.append(right[right_index])
right_index += 1

result.extend(left[left_index:])
result.extend(right[right_index:])
return result

if len(collection) <= 1:
Expand Down