Merge Sorted Arrays
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
mergedArrayis created with a length equal to the sum of the lengths ofnums1andnums2. This array will hold the merged result ofnums1andnums2.Copy nums1 to mergedArray: The elements of
nums1are copied intomergedArray, starting at the beginning ofmergedArray.Copy nums2 to mergedArray: The elements of
nums2are copied intomergedArray, starting at the position immediately after the last element ofnums1inmergedArray.Sort mergedArray: The
Array.Sortmethod is called to sortmergedArrayin non-decreasing order. This is necessary because the elements ofnums1andnums2were simply concatenated, not merged in a sorted manner.Return mergedArray: Finally,
mergedArrayis returned as the result of the method. This array contains all the elements ofnums1andnums2, sorted in non-decreasing order.
public class Solution
{
public int[] MergeArrays(int[] nums1, int[] nums2)
{
int[] mergedArray = new int[nums1.Length + nums2.Length];
nums1.CopyTo(mergedArray, 0);
nums2.CopyTo(mergedArray, nums1.Length);
Array.Sort(mergedArray);
return mergedArray;
}
}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
MergeArraysis defined with two parameters,nums1andnums2, which are the input arrays. It initializes a new arrayresultwith a length equal to the sum of the lengths ofnums1andnums2. Three pointersi,j, andkare also initialized to 0.iandjwill be used to traversenums1andnums2respectively, andkwill be used to insert elements intoresult.Traverse Both Arrays: The function enters a loop that continues as long as
iis less than the length ofnums1andjis 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 fromnums1is inserted intoresultat the position pointed to byk, andiandkare incremented. If not, the element fromnums2is inserted intoresult, andjandkare 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
nums1ornums2(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 fromnums1andnums2.
public int[] MergeArrays(int[] nums1, int[] nums2)
{
int[] result = new int[nums1.Length + nums2.Length];
int i = 0, j = 0, k = 0;
// Traverse both arrays
while (i < nums1.Length && j < nums2.Length)
{
// Check if current element of first array is smaller than current element of second array
if (nums1[i] < nums2[j])
result[k++] = nums1[i++];
else
result[k++] = nums2[j++];
}
// Store remaining elements of first array
while (i < nums1.Length)
result[k++] = nums1[i++];
// Store remaining elements of second array
while (j < nums2.Length)
result[k++] = nums2[j++];
return result;
}
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