Merge Two Sorted Arrays Without Extra Space in DSA Go - Time & Space Complexity
We want to understand how long it takes to merge two sorted arrays without using extra space.
How does the time needed grow as the arrays get bigger?
Analyze the time complexity of the following code snippet.
func merge(arr1 []int, arr2 []int) {
n, m := len(arr1), len(arr2)
for i := 0; i < n; i++ {
if arr1[i] > arr2[0] {
arr1[i], arr2[0] = arr2[0], arr1[i]
// Re-sort arr2's first element to keep arr2 sorted
first := arr2[0]
j := 1
for j < m && arr2[j] < first {
arr2[j-1] = arr2[j]
j++
}
arr2[j-1] = first
}
}
}
This code merges two sorted arrays by swapping elements and keeping the second array sorted without extra space.
- Primary operation: Outer loop runs over all elements of the first array.
- Nested operation: Inner loop shifts elements in the second array to keep it sorted after each swap.
- How many times: Outer loop runs n times; inner loop can run up to m times in worst case.
As the arrays get bigger, the number of operations grows roughly by multiplying their sizes.
| Input Size (n, m) | Approx. Operations |
|---|---|
| 10, 10 | Up to 100 operations |
| 100, 100 | Up to 10,000 operations |
| 1000, 1000 | Up to 1,000,000 operations |
Pattern observation: Operations grow roughly by the product of the sizes of the two arrays.
Time Complexity: O(n * m)
This means the time needed grows proportionally to the size of the first array times the size of the second array.
[X] Wrong: "Since both arrays are sorted, merging without extra space is always O(n + m)."
[OK] Correct: The inner shifting loop in the second array can run many times, making the total operations multiply, not just add.
Understanding this complexity helps you explain trade-offs when merging without extra space, a common real-world challenge.
"What if we used a gap method (Shell sort style) to reduce the inner loop work? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem requirement
The problem asks to merge two sorted arrays into a single sorted sequence without using extra space, meaning no new arrays should be created.Step 2: Identify the correct approach
Only Combine both arrays into one sorted sequence without using extra memory describes merging both arrays into one sorted sequence without extra memory, which matches the problem's goal.Final Answer:
Combine both arrays into one sorted sequence without using extra memory -> Option AQuick Check:
Merge without extra space = Combine sorted arrays in place [OK]
- Thinking extra arrays are allowed
- Sorting arrays separately without merging
- Copying elements without sorting
Solution
Step 1: Check Go syntax for swapping
In Go, swapping two variables can be done directly using multiple assignment: a[i], b[j] = b[j], a[i].Step 2: Analyze each option
a[i], b[j] = b[j], a[i] uses correct Go syntax. temp := a[i]; a[i] = b[j]; b[j] = temp uses a temporary variable which is valid but uses extra space. a[i] = b[j]; b[j] = a[i] overwrites a[i] before assigning b[j]. a[i] = a[i] + b[j]; b[j] = a[i] - b[j]; a[i] = a[i] - b[j] uses arithmetic swap which is error-prone and not recommended.Final Answer:
a[i], b[j] = b[j], a[i] -> Option DQuick Check:
Go swap syntax = a[i], b[j] = b[j], a[i] [OK]
- Overwriting values before swapping
- Using temp variables when avoiding extra space
- Using arithmetic swaps that can cause errors
arr1 := []int{1, 5, 9, 10, 15, 20}
arr2 := []int{2, 3, 8, 13}
for i := 0; i < len(arr1); i++ {
if arr1[i] > arr2[0] {
arr1[i], arr2[0] = arr2[0], arr1[i]
// Re-sort arr2's first element
first := arr2[0]
k := 1
for k < len(arr2) && arr2[k] < first {
arr2[k-1] = arr2[k]
k++
}
arr2[k-1] = first
}
}
fmt.Println(arr1)
fmt.Println(arr2)Solution
Step 1: Understand the merging logic
The code compares each element of arr1 with the first element of arr2. If arr1[i] is greater, it swaps and then re-sorts arr2's first element to maintain sorted order.Step 2: Trace the arrays after merging
After the loop, arr1 becomes [1 2 3 5 8 9] and arr2 becomes [10 13 15 20]. The code does not combine arrays but rearranges elements to keep both sorted without extra space.Final Answer:
[1 2 3 5 8 9] [10 13 15 20] -> Option AQuick Check:
In-place merge = arr1 and arr2 sorted separately [OK]
- Assuming arrays combine into one
- Not re-sorting arr2 after swap
- Confusing output format
for i := 0; i < len(arr1); i++ {
if arr1[i] > arr2[0] {
arr1[i], arr2[0] = arr2[0], arr1[i]
for j := 0; j < 2; j++ {
if arr2[j] > arr2[j+1] {
arr2[j], arr2[j+1] = arr2[j+1], arr2[j]
}
}
}
}Solution
Step 1: Analyze the sorting of arr2 after swap
The inner loop only runs once per swap and performs a single pass of adjacent swaps, which is not enough to fully sort arr2.Step 2: Identify the bug
Because arr2 is not fully sorted after swapping, the merge will fail to maintain sorted order. A full re-sort or a proper insertion step is needed.Final Answer:
The inner loop does not fully sort arr2 after swapping -> Option CQuick Check:
Partial sort after swap causes incorrect order [OK]
- Assuming one pass sorts arr2
- Swapping incorrectly
- Looping over wrong array
Solution
Step 1: Merge by comparing and swapping elements
Compare elements from arr1 and arr2. Swap if arr1[i] > arr2[0], then re-sort arr2 to keep it sorted.Step 2: Final arrays after merging
After merging, arr1 holds the smallest 5 elements: [1, 2, 3, 4, 7], and arr2 holds the remaining sorted elements: [8, 9, 10].Final Answer:
arr1 = [1, 2, 3, 4, 7], arr2 = [8, 9, 10] -> Option BQuick Check:
In-place merge keeps first array smallest elements [OK]
- Not re-sorting arr2 after swaps
- Mixing order of elements in arr2
- Assuming arr1 grows beyond its size
Ready to prove it?
Pass the test and this pattern moves unknown
