Bird
0
0
DSA Cprogramming

Merge Two Sorted Arrays Without Extra Space in DSA C

Choose your learning style9 modes available
Mental Model
We combine two sorted lists into one sorted order without using extra storage by swapping and sorting in place.
Analogy: Imagine two sorted stacks of books on two tables. Instead of moving all books to a new table, you swap books between tables to keep both stacks sorted without extra space.
Array1: [1, 5, 9, 10, 15, 20]
Array2: [2, 3, 8, 13]
Dry Run Walkthrough
Input: Array1: [1, 5, 9, 10, 15, 20], Array2: [2, 3, 8, 13]
Goal: Merge both arrays so that all elements are sorted across both arrays without using extra space.
Step 1: Compare last element of Array1 (20) with first element of Array2 (2), swap because 20 > 2
Array1: [1, 5, 9, 10, 15, 2]
Array2: [20, 3, 8, 13]
Why: We ensure smaller elements stay in Array1 and larger in Array2 by swapping out-of-place elements.
Step 2: Sort Array1 and Array2 individually after swap
Array1: [1, 2, 5, 9, 10, 15]
Array2: [3, 8, 13, 20]
Why: Sorting each array restores order after swapping.
Step 3: Compare last element of Array1 (15) with first element of Array2 (3), swap because 15 > 3
Array1: [1, 2, 5, 9, 10, 3]
Array2: [15, 8, 13, 20]
Why: Continue swapping to maintain sorted order across arrays.
Step 4: Sort Array1 and Array2 again
Array1: [1, 2, 3, 5, 9, 10]
Array2: [8, 13, 15, 20]
Why: Restore sorted order after swap.
Step 5: Compare last element of Array1 (10) with first element of Array2 (8), swap because 10 > 8
Array1: [1, 2, 3, 5, 8, 10]
Array2: [9, 13, 15, 20]
Why: Final swap to ensure all smaller elements are in Array1.
Step 6: Sort Array1 and Array2 one last time
Array1: [1, 2, 3, 5, 8, 9]
Array2: [10, 13, 15, 20]
Why: Final sorted arrays without extra space.
Result:
Array1: [1, 2, 3, 5, 8, 9]
Array2: [10, 13, 15, 20]
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

void sort_array(int *arr, int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

void merge_without_extra_space(int *arr1, int size1, int *arr2, int size2) {
    for (int i = 0; i < size1; i++) {
        if (arr1[size1 - 1] > arr2[0]) {
            int temp = arr1[size1 - 1];
            arr1[size1 - 1] = arr2[0];
            arr2[0] = temp;
            sort_array(arr1, size1);
            sort_array(arr2, size2);
        }
    }
}

void print_array(int *arr, int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr1[] = {1, 5, 9, 10, 15, 20};
    int arr2[] = {2, 3, 8, 13};
    int size1 = sizeof(arr1) / sizeof(arr1[0]);
    int size2 = sizeof(arr2) / sizeof(arr2[0]);

    merge_without_extra_space(arr1, size1, arr2, size2);

    print_array(arr1, size1);
    print_array(arr2, size2);

    return 0;
}
if (arr1[size1 - 1] > arr2[0]) {
Check if last element of first array is greater than first element of second array to decide swap
int temp = arr1[size1 - 1]; arr1[size1 - 1] = arr2[0]; arr2[0] = temp;
Swap the out-of-place elements to maintain sorted order across arrays
sort_array(arr1, size1); sort_array(arr2, size2);
Sort both arrays after swap to restore order
OutputSuccess
1 2 3 5 8 9 10 13 15 20
Complexity Analysis
Time: O(n * m * (n + m)) because for each element in first array we may swap and sort both arrays which costs O(n + m) each time
Space: O(1) because no extra arrays or data structures are used, only swaps and in-place sorting
vs Alternative: Naive approach uses extra space O(n + m) to merge and then sort once, which is faster but uses more memory
Edge Cases
One or both arrays are empty
No swaps or sorting needed, arrays remain as is
DSA C
for (int i = 0; i < size1; i++) { if (arr1[size1 - 1] > arr2[0]) { ... } }
Arrays already fully sorted with no overlapping values
No swaps occur, arrays remain unchanged
DSA C
if (arr1[size1 - 1] > arr2[0]) {
Arrays with duplicate values
Duplicates are handled naturally by sorting after swaps
DSA C
sort_array(arr1, size1); sort_array(arr2, size2);
When to Use This Pattern
When asked to merge two sorted arrays without extra space, use swapping and in-place sorting to maintain order without additional memory.
Common Mistakes
Mistake: Trying to merge by shifting elements manually without sorting after swaps
Fix: Always sort both arrays after swapping to restore sorted order
Mistake: Not comparing last element of first array with first element of second array
Fix: Compare arr1[size1 - 1] with arr2[0] to find out-of-place elements
Summary
Merges two sorted arrays into sorted order without using extra space by swapping and sorting in place.
Use when memory is limited and you cannot allocate extra arrays for merging.
The key insight is to swap out-of-place elements between arrays and then sort each array to maintain order.