📖 Scenario: You have two sorted arrays representing two sorted lists of numbers. You want to merge them into one sorted sequence without using extra space, so the first array holds the smallest elements and the second array holds the remaining larger elements.
🎯 Goal: Build a program that merges two sorted arrays arr1 and arr2 without using any extra array or list. After merging, arr1 should contain the smallest elements and arr2 the larger elements, both sorted.
📋 What You'll Learn
Create two sorted integer arrays arr1 and arr2 with exact values
Create an integer variable n for the length of arr1
Implement the merge logic without extra space using nested loops and swapping
Print both arrays after merging to show the final sorted state
💡 Why This Matters
🌍 Real World
Merging sorted data streams or lists without extra memory is useful in embedded systems or memory-limited environments.
💼 Career
Understanding in-place merging is important for software engineers working on performance-critical applications and systems programming.
Progress0 / 4 steps
1
Create Two Sorted Arrays
Create two sorted integer arrays called arr1 and arr2 with these exact values: arr1 = new int[] {1, 5, 9, 10, 15, 20} and arr2 = new int[] {2, 3, 8, 13}.
DSA C#
Hint
Use int[] arr1 = new int[] { ... }; and int[] arr2 = new int[] { ... }; to create the arrays.
2
Create Length Variable for First Array
Create an integer variable called n and set it to the length of arr1 using arr1.Length.
DSA C#
Hint
Use int n = arr1.Length; to get the length of the first array.
3
Merge Arrays Without Extra Space
Use nested for loops with variables i and j to iterate over arr1 and arr2. Inside the inner loop, if arr1[i] > arr2[j], swap the elements using a temporary variable temp.
DSA C#
Hint
Use two for loops: outer loop with i from 0 to n, inner loop with j from 0 to arr2.Length. Swap when arr1[i] is greater than arr2[j].
4
Print Both Arrays After Merging
Print the elements of arr1 and arr2 after merging using two for loops. Use Console.Write to print elements separated by spaces and Console.WriteLine() to move to the next line after each array.
DSA C#
Hint
Use two for loops to print arr1 and arr2 elements separated by spaces. Use Console.WriteLine() after each array.
Practice
(1/5)
1. What is the main goal when merging two sorted arrays without extra space?
easy
A. Create a new array to hold merged elements
B. Combine both arrays into one sorted sequence without using extra memory
C. Copy elements from one array to another without sorting
D. Sort each array separately using extra space
Solution
Step 1: Understand the problem requirement
The task is to merge two sorted arrays so that the combined elements remain sorted, but without using any extra space like a new array.
Step 2: Identify the correct approach
We must rearrange elements within the given arrays themselves to maintain sorted order without additional memory.
Final Answer:
Combine both arrays into one sorted sequence without using extra memory -> Option B
Quick Check:
Merge without extra space = Combine in place [OK]
Hint: Merge by swapping elements between arrays without extra space [OK]
Common Mistakes:
Assuming extra space is allowed
Sorting arrays separately without merging
Creating a new array for merged result
2. Which of the following C# code snippets correctly swaps elements between two arrays without extra space?
In-place merge with insertion sort = arr1 and arr2 sorted [OK]
Hint: Swap if arr1[i] > arr2[0], then insert arr2[0] sorted [OK]
Common Mistakes:
Not inserting arr2[0] correctly after swap
Overwriting arr2 elements incorrectly
Confusing array indices during insertion
4. Identify the error in this code snippet for merging two sorted arrays without extra space:
for (int i = 0; i < n; i++) {
if (arr1[i] > arr2[0]) {
int temp = arr1[i];
arr1[i] = arr2[0];
arr2[0] = temp;
int first = arr2[0];
int k = 1;
while (k < m && arr2[k] < first) {
arr2[k - 1] = arr2[k];
k++;
}
arr2[k] = first;
}
}
medium
A. The insertion index in arr2 is incorrect; should be arr2[k - 1] = first
B. The swap logic between arr1[i] and arr2[0] is wrong
C. The loop should run from i = 1 instead of 0
D. The condition arr2[k] < first should be arr2[k] > first
Solution
Step 1: Analyze insertion logic in arr2
The code shifts elements left to make space for 'first'. The correct insertion index after shifting is k - 1, not k.
Step 2: Identify the error impact
Assigning arr2[k] = first overwrites the next element incorrectly, causing wrong order.
Final Answer:
The insertion index in arr2 is incorrect; should be arr2[k - 1] = first -> Option A
Quick Check:
Correct insertion index = k - 1 [OK]
Hint: Insert shifted element at k-1 index after loop ends [OK]
Common Mistakes:
Using wrong index for insertion after shifting
Incorrect swap logic
Wrong loop start index
5. Given two sorted arrays: arr1 = [1, 4, 7, 8, 10] arr2 = [2, 3, 9] How would you merge them without extra space so that after merging, arr1 contains the first 5 smallest elements and arr2 contains the remaining sorted elements? Choose the correct final arrays.
hard
A. arr1 = [1, 2, 3, 4, 7], arr2 = [8, 9, 10]
B. arr1 = [1, 3, 4, 7, 8], arr2 = [2, 9, 10]
C. arr1 = [1, 2, 4, 7, 8], arr2 = [3, 9, 10]
D. arr1 = [1, 2, 3, 4, 8], arr2 = [7, 9, 10]
Solution
Step 1: Compare and swap elements between arr1 and arr2
Start from beginning of arr1, if arr1[i] > arr2[0], swap and then insert arr2[0] in sorted order in arr2.
Step 2: After all swaps and insertions
arr1 becomes [1, 2, 3, 4, 7] and arr2 becomes [8, 9, 10], maintaining sorted order without extra space.