From 3a2275c5bcece97310860d5ab09fa642a5493cc9 Mon Sep 17 00:00:00 2001 From: Yashasvi-dot-V Date: Fri, 13 Mar 2026 12:46:07 +0530 Subject: [PATCH] Fix merge sort remaining elements merge logic --- sorts/merge_sort.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index ca4d319..359609a 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -40,7 +40,7 @@ def merge_sort(collection): left_length = len(left_half) right_length = len(right_half) while i < left_length and j < right_length: - if left_half[i] < right_half[j]: + if left_half[i] <= right_half[j]: collection[k] = left_half[i] i += 1 else: @@ -48,15 +48,10 @@ def merge_sort(collection): j += 1 k += 1 - while i < left_length: - collection[k] = left_half[i] - i += 1 - k += 1 - - while j < right_length: - collection[k] = right_half[j] - j += 1 - k += 1 + if i < left_length: + collection[k:]=left_half[i:] + elif j < right_length: + collection[k:]=right_half[j:] return collection