Merge Sorted Arrays: A Simple Guide With Javascript
Imagine you’re a librarian, and you have two piles of books sorted by title. Your task is to merge these two piles into one, while keeping the order. This is similar to the problem we’re going to solve today: merging two sorted arrays.
In this article, we’ll be using JavaScript to solve this problem. Why JavaScript? Well, JavaScript has a built-in function called splice()
that can insert an element into a specific position in an array, which makes our job a lot easier.
Let’s dive into the problem:
We have two sorted arrays nums1
and nums2
, and two integers m
and n
representing the number of elements in nums1
and nums2
respectively. We need to merge nums2
into nums1
so that the merged array is also sorted. The twist here is that we’re not supposed to return the merged array, but instead modify nums1
in-place.
Here’s how we can do it:
var merge = function(nums1, m, nums2, n) {
nums1.splice(-n, n); // In JavaScript, arrays are dynamic. We don't need the trailing zeros.
let i = 0; // This keeps track of the current position in nums1
for (let n2 of nums2){
for(;nums1[i]<n2 && i<m;i++){ // Skip the positions in nums1 that are smaller than the current element in nums2
}
nums1.splice(i, 0, n2); // We've found the insertion position, let's insert the element
m++; // The total number of elements has changed
}
};
This algorithm works by iterating over nums2
and for each element, it finds the correct position in nums1
and inserts it there. The time complexity of the algorithm is $O(m + n)$, where $m$ and $n$ are the lengths of nums1
and nums2
respectively. This is because for each element in nums2
, we may have to iterate over all elements in nums1
to find the correct position.
So, next time you’re asked to merge two sorted arrays, remember our librarian and the splice()
function. Happy coding!