LeetCode 88: Merge Sorted Array | Default Sort Method, In-Place Merge Approaches | C++, Java, Python

Published: 23 November 2024
on channel: Algo-holic
71
7

🔍 Merge Sorted Array Problem Solution
We are given two sorted arrays nums1 and nums2 and their respective lengths m and n . The task is to merge them into nums1 in-place in sorted order.

We’ll explore three approaches to solve this problem:
1️⃣ Default Sort Function
2️⃣ Merge Process from Merge Sort
3️⃣ In-Place Merge Starting from the End

🧠 Default Sort Function
Intuition
Since the problem involves merging two arrays and sorting them, the simplest approach is to append nums2 to nums1 and use the built-in sort function. This is straightforward and relies on language-provided tools for sorting.

Approach
Replace the trailing zeroes in nums1 with elements from nums2.
Use the built-in sort() function to sort the combined array in-place.

Complexity
Time Complexity: O((m+n) log (m+n))
Space Complexity: O(1)

---

🧠 Using Merge Process of Merge Sort
Intuition
The merging step in merge sort combines two sorted arrays into a single sorted array. By maintaining two pointers—one for each array—we can compare elements at these pointers and transfer the smaller element into a new result array. This ensures a linear time complexity.

Approach
Use two pointers, i starting at the beginning of nums1 and j at the beginning of nums2.
Compare elements at nums1[i] and nums2[j]. Add the smaller element to a new array and move the corresponding pointer forward.
Once one array is fully processed, append the remaining elements from the other array to the result.
Copy the elements from the new array back into nums1.

Complexity
Time Complexity: O(n)
Space Complexity: O(n)

🧠 In-Place Merge Starting from the End
Intuition
To eliminate the need for extra space, we can merge the arrays directly into nums1 starting from the end. This way, the larger elements are placed at the end of nums1 without needing to shift other elements. The extra zeroes at the end of nums1 help accommodate this approach.

Approach
Start with i = m - 1 (pointing to the last actual element in nums1) and j = n - 1 (pointing to the last element in nums2).
Compare elements at nums1[i] and nums2[j]. Place the larger of the two at nums1[k], and decrement the respective pointer (i or j).
If any elements remain in nums2, copy them into nums1.

Complexity
Time Complexity: O(n)
Space Complexity: O(1)

---

📂 Links and Resources
💡 Learn not just the solutions but the thought process behind solving problems at https://Algo-holic.com
📘 GitHub Repository : (https://github.com/akash-onepercent/L...)
🚀 Check out My Journey on LeetCode : (https://leetcode.com/u/akash-onepercent/)
🤝 Let’s Connect on LinkedIn : (  / akash-onepercent  )

🤔 Feedback and Suggestions
I hope this video helps you solve the problem with confidence!
If you have any feedback or suggestions, drop them in the comments below. Your input helps me improve!

Happy coding, and I’ll see you in the next problem! 😊


On this page of the site you can watch the video online LeetCode 88: Merge Sorted Array | Default Sort Method, In-Place Merge Approaches | C++, Java, Python with a duration of online in good quality, which was uploaded by the user Algo-holic 23 November 2024, share the link with friends and acquaintances, this video has already been watched 71 times on youtube and it was liked by 7 viewers. Enjoy your viewing!