From c2b2c4db285a9c7fe5df994b339f1fc9e7e6d9a1 Mon Sep 17 00:00:00 2001 From: "Biswayan B.Mal" <116450061+30November@users.noreply.github.com> Date: Sat, 28 Jun 2025 19:32:21 +0530 Subject: [PATCH 1/2] merge_sort.py Here after looking the actual code, I found out that the storing the sorted element in the result one by one on comparing two lists, that is left and right, it was using the pop() function for the seek of reducing the line of code. I wanted to alert about the pop function, as pop function in python extract the element on particular valid index from the list, mentioning in argument (by default the last element of the list). But pop off the first element takes O(n) complexity (where n is the length of the list) which leads to extra time complexity, also kind of making worst case for already sorted list. Also, there is no need of extracting element, instead we just access the element through pointer ( left_index , right_index ) --- sorts/merge_sort.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 0628b848b794..22e430d853da 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -37,10 +37,20 @@ 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_length = len(left) + right_length = len(right) + left_index = right_index = 0 + + while (left_index < left_length) and (right_index < right_length): + 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 ab95c61fba1843a0cfa704d42d07e0d0c8e82149 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Jun 2025 14:16:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/merge_sort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 22e430d853da..d7e9ba29cfcb 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -40,7 +40,7 @@ def merge(left: list, right: list) -> list: left_length = len(left) right_length = len(right) left_index = right_index = 0 - + while (left_index < left_length) and (right_index < right_length): if left[left_index] < right[right_index]: result.append(left[left_index]) @@ -48,9 +48,9 @@ def merge(left: list, right: list) -> list: else: 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: