From a59bc6052c38a6102e8ad43470b3e7dc54878a36 Mon Sep 17 00:00:00 2001 From: JainRamyak Date: Sun, 19 Oct 2025 01:41:47 +0530 Subject: [PATCH 1/5] Add merge sorted array algorithm --- data_structures/arrays/merge_sorted.py | 59 ++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 data_structures/arrays/merge_sorted.py diff --git a/data_structures/arrays/merge_sorted.py b/data_structures/arrays/merge_sorted.py new file mode 100644 index 000000000000..a23c984b7b3c --- /dev/null +++ b/data_structures/arrays/merge_sorted.py @@ -0,0 +1,59 @@ +def merge_sorted_arrays(nums1: list[int], nums2: list[int]) -> list[int]: + """ + Merge two sorted arrays into one sorted array. + + Args: + nums1: The first sorted array. + nums2: The second sorted array. + + Returns: + A single merged and sorted array. + + Examples: + >>> merge_sorted_arrays([1, 3, 5], [2, 4, 6]) + [1, 2, 3, 4, 5, 6] + + >>> merge_sorted_arrays([1, 2], []) + [1, 2] + + >>> merge_sorted_arrays([], [3, 4]) + [3, 4] + + >>> merge_sorted_arrays([], []) + [] + + >>> merge_sorted_arrays([0, 0], [0, 0]) + [0, 0, 0, 0] + + >>> merge_sorted_arrays([-5, -3, -1], [-2, -2]) + [-5, -3, -2, -2, -1] + """ + # If one array is empty, simply return the other. + if not nums1: + return nums2 + if not nums2: + return nums1 + + # Two-pointer approach to merge both sorted arrays. + i, j = 0, 0 + merged = [] + + while i < len(nums1) and j < len(nums2): + if nums1[i] <= nums2[j]: + merged.append(nums1[i]) + i += 1 + else: + merged.append(nums2[j]) + j += 1 + + # Append remaining elements if any. + merged.extend(nums1[i:]) + merged.extend(nums2[j:]) + + return merged + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 97b3c339d2c7b3fcf6a9f016f8bbabf501e353f0 Mon Sep 17 00:00:00 2001 From: JainRamyak Date: Sun, 7 Jun 2026 07:43:54 +0000 Subject: [PATCH 2/5] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index daf71bab8162..85c1cf0ded6d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -190,6 +190,7 @@ * [Index 2D Array In 1D](data_structures/arrays/index_2d_array_in_1d.py) * [Kth Largest Element](data_structures/arrays/kth_largest_element.py) * [Median Two Array](data_structures/arrays/median_two_array.py) + * [Merge Sorted](data_structures/arrays/merge_sorted.py) * [Monotonic Array](data_structures/arrays/monotonic_array.py) * [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py) * [Permutations](data_structures/arrays/permutations.py) From 743becc2078af043c01567a70e873016be4bc3c6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Sep 2026 19:27:18 +0200 Subject: [PATCH 3/5] Validate input arrays for sorting in merge function Add validation to check if input arrays are sorted. --- data_structures/arrays/merge_sorted.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/data_structures/arrays/merge_sorted.py b/data_structures/arrays/merge_sorted.py index a23c984b7b3c..0a5ea9e84909 100644 --- a/data_structures/arrays/merge_sorted.py +++ b/data_structures/arrays/merge_sorted.py @@ -27,7 +27,21 @@ def merge_sorted_arrays(nums1: list[int], nums2: list[int]) -> list[int]: >>> merge_sorted_arrays([-5, -3, -1], [-2, -2]) [-5, -3, -2, -2, -1] + >>> merge_sorted_arrays(range(5), range(5)) + [0, 0, 1, 1, 2, 2, 3, 3, 4, 4] + >>> merge_sorted_arrays([1, -1], []) + Traceback (most recent call last): + ... + ValueError: nums = [1, -1] is not sorted + >>> merge_sorted_arrays([], [1, -1]) + Traceback (most recent call last): + ... + ValueError: nums = [1, -1] is not sorted """ + for nums in (nums1, nums2): + if list(nums1) != sorted(nums1): + msg f"{nums1 = } is not sorted" + raise ValueError(msg) # If one array is empty, simply return the other. if not nums1: return nums2 From ea2a6ea5df17e53218b052a87aed1704c191ca3c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Sep 2026 19:29:21 +0200 Subject: [PATCH 4/5] Correct error message assignment in merge_sorted_arrays Fix variable assignment for error message in merge_sorted_arrays function. --- data_structures/arrays/merge_sorted.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/merge_sorted.py b/data_structures/arrays/merge_sorted.py index 0a5ea9e84909..4a2cf479015f 100644 --- a/data_structures/arrays/merge_sorted.py +++ b/data_structures/arrays/merge_sorted.py @@ -27,12 +27,15 @@ def merge_sorted_arrays(nums1: list[int], nums2: list[int]) -> list[int]: >>> merge_sorted_arrays([-5, -3, -1], [-2, -2]) [-5, -3, -2, -2, -1] + >>> merge_sorted_arrays(range(5), range(5)) [0, 0, 1, 1, 2, 2, 3, 3, 4, 4] + >>> merge_sorted_arrays([1, -1], []) Traceback (most recent call last): ... ValueError: nums = [1, -1] is not sorted + >>> merge_sorted_arrays([], [1, -1]) Traceback (most recent call last): ... @@ -40,7 +43,7 @@ def merge_sorted_arrays(nums1: list[int], nums2: list[int]) -> list[int]: """ for nums in (nums1, nums2): if list(nums1) != sorted(nums1): - msg f"{nums1 = } is not sorted" + msg = f"{nums1 = } is not sorted" raise ValueError(msg) # If one array is empty, simply return the other. if not nums1: From c46418165e9bc2da6555a5c0efa08273fa197c14 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Sep 2026 19:31:37 +0200 Subject: [PATCH 5/5] Fix sorted check for input arrays in merge function --- data_structures/arrays/merge_sorted.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/merge_sorted.py b/data_structures/arrays/merge_sorted.py index 4a2cf479015f..cef98fe3b435 100644 --- a/data_structures/arrays/merge_sorted.py +++ b/data_structures/arrays/merge_sorted.py @@ -42,8 +42,8 @@ def merge_sorted_arrays(nums1: list[int], nums2: list[int]) -> list[int]: ValueError: nums = [1, -1] is not sorted """ for nums in (nums1, nums2): - if list(nums1) != sorted(nums1): - msg = f"{nums1 = } is not sorted" + if list(nums) != sorted(nums): + msg = f"{nums = } is not sorted" raise ValueError(msg) # If one array is empty, simply return the other. if not nums1: