0
0
DSA Pythonprogramming~10 mins

Merge Two Sorted Arrays Without Extra Space in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Merge Two Sorted Arrays Without Extra Space
Start with two sorted arrays A and B
Compare last element of A with first element of B
If A's last > B's first?
YesSwap elements
Sort array B
Sort array A
Repeat until no swaps
Merged arrays
We repeatedly compare and swap elements between the two arrays to ensure all smaller elements are in the first array and larger in the second, sorting each after swaps until fully merged without extra space.
Execution Sample
DSA Python
A = [1, 5, 9, 10, 15, 20]
B = [2, 3, 8, 13]
i = len(A) - 1
j = 0
while i >= 0 and j < len(B):
    if A[i] > B[j]:
        A[i], B[j] = B[j], A[i]
    i -= 1
    j += 1
A.sort()
B.sort()
This code merges two sorted arrays A and B without extra space by swapping and sorting.
Execution Table
StepOperationijA ArrayB ArraySwap HappenedNotes
1Compare A[5]=20 and B[0]=250[1, 5, 9, 10, 15, 20][2, 3, 8, 13]Yes20 > 2, swap A[5] and B[0]
2After swap50[1, 5, 9, 10, 15, 2][20, 3, 8, 13]N/AArrays updated
3Decrement i, increment j41[1, 5, 9, 10, 15, 2][20, 3, 8, 13]N/APrepare next comparison
4Compare A[4]=15 and B[1]=341[1, 5, 9, 10, 15, 2][20, 3, 8, 13]Yes15 > 3, swap A[4] and B[1]
5After swap41[1, 5, 9, 10, 3, 2][20, 15, 8, 13]N/AArrays updated
6Decrement i, increment j32[1, 5, 9, 10, 3, 2][20, 15, 8, 13]N/APrepare next comparison
7Compare A[3]=10 and B[2]=832[1, 5, 9, 10, 3, 2][20, 15, 8, 13]Yes10 > 8, swap A[3] and B[2]
8After swap32[1, 5, 9, 8, 3, 2][20, 15, 10, 13]N/AArrays updated
9Decrement i, increment j23[1, 5, 9, 8, 3, 2][20, 15, 10, 13]N/APrepare next comparison
10Compare A[2]=9 and B[3]=1323[1, 5, 9, 8, 3, 2][20, 15, 10, 13]No9 <= 13, no swap
11Decrement i, increment j14[1, 5, 9, 8, 3, 2][20, 15, 10, 13]N/Aj out of range, stop loop
12Sort A--[1, 2, 3, 5, 8, 9][20, 15, 10, 13]N/ASort first array
13Sort B--[1, 2, 3, 5, 8, 9][10, 13, 15, 20]N/ASort second array
14Final merged arrays--[1, 2, 3, 5, 8, 9][10, 13, 15, 20]N/AArrays merged without extra space
💡 Loop ends when j reaches length of B (4), no more elements to compare.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7After Step 10Final
i554321
j001234
A[1,5,9,10,15,20][1,5,9,10,15,2][1,5,9,10,3,2][1,5,9,8,3,2][1,5,9,8,3,2][1,2,3,5,8,9]
B[2,3,8,13][20,3,8,13][20,15,8,13][20,15,10,13][20,15,10,13][10,13,15,20]
Key Moments - 3 Insights
Why do we compare A's last element with B's first element?
Because A and B are sorted, the largest element in A is at the end and the smallest in B is at the start. Swapping these ensures smaller elements move to A and larger to B, as shown in steps 1 and 4.
Why do we sort arrays after swapping instead of during?
Sorting after all swaps is efficient because swapping only moves some elements out of order. Sorting at the end (steps 12 and 13) restores order without extra space.
What stops the swapping loop?
The loop stops when j reaches the length of B (step 11), meaning all elements in B have been compared and swapped if needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4, what are the contents of arrays A and B?
AA = [1, 5, 9, 10, 3, 2], B = [20, 15, 8, 13]
BA = [1, 5, 9, 10, 15, 2], B = [20, 3, 8, 13]
CA = [1, 5, 9, 10, 15, 20], B = [2, 3, 8, 13]
DA = [1, 5, 9, 8, 3, 2], B = [20, 15, 10, 13]
💡 Hint
Check the 'After swap' row after step 1 in the execution_table.
At which step does the loop stop because j reaches the length of B?
AStep 10
BStep 12
CStep 11
DStep 13
💡 Hint
Look at the 'Notes' column in the execution_table for the step where j goes out of range.
If we did not sort arrays after swapping, what would be the state of array A at the end of swaps?
A[1, 5, 9, 8, 3, 2]
B[20, 15, 10, 13]
C[1, 2, 3, 5, 8, 9]
D[1, 5, 9, 10, 15, 20]
💡 Hint
Check the array A state at step 10 before sorting in the execution_table.
Concept Snapshot
Merge Two Sorted Arrays Without Extra Space:
- Compare last of A with first of B
- Swap if A's element is bigger
- Move pointers i (end of A) and j (start of B)
- Repeat until no swaps possible
- Sort both arrays at the end
- Result: merged arrays without extra space
Full Transcript
This concept shows how to merge two sorted arrays without using extra space. We start by comparing the last element of the first array with the first element of the second array. If the element in the first array is bigger, we swap them. We then move the pointers inward and repeat this process. After all swaps, we sort both arrays to restore order. This way, all smaller elements end up in the first array and larger ones in the second, achieving a merged result without extra memory. The execution table traces each step, showing swaps and array states. Key moments clarify why we compare these elements and why sorting is done after swapping. The visual quiz tests understanding of array states and loop termination.