Merge Sorted Arrays
Array, Two Pointers
Given two integer arrays nums1
and nums2
, sorted in non-decreasing order.
Solutions
Approach - Brute Force Technique
This method also returns a new array that is a sorted merge of the input arrays. However, it does this by first concatenating the two input arrays into one larger array, and then sorting this larger array.
Steps
Initialize mergedArray: A new integer array
mergedArray
is created with a length equal to the sum of the lengths ofnums1
andnums2
. This array will hold the merged result ofnums1
andnums2
.Copy nums1 to mergedArray: The elements of
nums1
are copied intomergedArray
, starting at the beginning ofmergedArray
.Copy nums2 to mergedArray: The elements of
nums2
are copied intomergedArray
, starting at the position immediately after the last element ofnums1
inmergedArray
.Sort mergedArray: The
Array.Sort
method is called to sortmergedArray
in non-decreasing order. This is necessary because the elements ofnums1
andnums2
were simply concatenated, not merged in a sorted manner.Return mergedArray: Finally,
mergedArray
is returned as the result of the method. This array contains all the elements ofnums1
andnums2
, sorted in non-decreasing order.
Complexity
Time complexity:
O((N+M) log (N+M))
Space complexity:
O(N+M)
The time complexity of the MergeArrays
method can be more accurately described as O((N+M) log (N+M)), where N is the size of the first array and M is the size of the second array. This is because the Array.Sort
function is called on the merged array, which has a size of N+M
, and the time complexity of sorting is O(n log n)
, where n is the size of the array being sorted. So in this case, n is N+M. Therefore, the time complexity is O((N+M) log (N+M))
.
The space complexity of the algorithm is O(N + M). This is because a new array of size N + M
is created to store the result. The space used by this array grows linearly with the size of the input arrays, hence the linear space complexity.
Approach - Two Pointers
This method works by maintaining two pointers, one for each input array. The pointers are initially set to the start of their respective arrays. The method then compares the elements at the current pointer positions in both arrays, and the smaller of the two elements is added to the merged array. The pointer for the array from which an element was taken is then incremented. If one of the arrays is exhausted before the other, the remaining elements from the non-exhausted array are copied into the merged array. The method finally returns the merged array. This technique ensures that the merged array is also sorted in non-decreasing order.
Steps
Initialization: The function
MergeArrays
is defined with two parameters,nums1
andnums2
, which are the input arrays. It initializes a new arrayresult
with a length equal to the sum of the lengths ofnums1
andnums2
. Three pointersi
,j
, andk
are also initialized to 0.i
andj
will be used to traversenums1
andnums2
respectively, andk
will be used to insert elements intoresult
.Traverse Both Arrays: The function enters a loop that continues as long as
i
is less than the length ofnums1
andj
is less than the length ofnums2
. This ensures that we only consider elements that are within the bounds of both arrays.Compare and Insert Elements: Inside the loop, the function checks if the current element of
nums1
(pointed to byi
) is less than the current element ofnums2
(pointed to byj
). If it is, the element fromnums1
is inserted intoresult
at the position pointed to byk
, andi
andk
are incremented. If not, the element fromnums2
is inserted intoresult
, andj
andk
are incremented. This ensures that the smaller of the two current elements is always inserted intoresult
.Store Remaining Elements: After the loop, there may be remaining elements in
nums1
ornums2
(but not both). Two additional loops are used to insert any such remaining elements intoresult
. The first loop inserts remaining elements fromnums1
, and the second loop inserts remaining elements fromnums2
.Return Result: Finally, the function returns
result
, which is a sorted array containing all elements fromnums1
andnums2
.
Complexity
Time complexity:
O(N+M)
Space complexity:
O(N+M)
The time complexity of the merge algorithm is O(n + m), where n and m are the lengths of the two input arrays, nums1
and nums2
. This is because in the worst case, the algorithm needs to iterate over all elements in both arrays once.
The space complexity of the algorithm is O(n + m). This is because a new array of size n + m
is created to store the result. The space used by this array grows linearly with the size of the input arrays, hence the linear space complexity. The rest of the variables used in the algorithm (i
, j
, and k
) use a constant amount of space.
Last updated