From 4dacca55a383d34cd1070176372c227e863c10fe Mon Sep 17 00:00:00 2001 From: 30November Date: Sat, 12 Jul 2025 14:13:37 +0530 Subject: [PATCH 1/3] Better --- sorts/merge_sort.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 0628b848b794..7750c3ed2d1e 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -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: From 47b3ea8fe0e77035729cee07595de9cdc5962b9e Mon Sep 17 00:00:00 2001 From: 30November Date: Sat, 12 Jul 2025 14:23:54 +0530 Subject: [PATCH 2/3] Whitespace Remove --- sorts/merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 7750c3ed2d1e..53c6ac21d2ae 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -39,7 +39,7 @@ def merge(left: list, right: list) -> list: result = [] 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]) From 60f0bb98f00f004baf16945a5f0764667b6eb45f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Jul 2025 08:55:10 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/merge_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 53c6ac21d2ae..7bbbe46e6a46 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -38,7 +38,7 @@ def merge(left: list, right: list) -> list: """ result = [] left_index = right_index = 0 - length_left , length_right = len(left) , len(right) + 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]: @@ -48,8 +48,8 @@ def merge(left: list, right: list) -> list: result.append(right[right_index]) right_index += 1 - result.extend(left[left_index: ]) - result.extend(right[right_index: ]) + result.extend(left[left_index:]) + result.extend(right[right_index:]) return result if len(collection) <= 1: