C Exercises: Merge one sorted array into another sorted array
38. Merge Sorted Arrays
Write a program in C to merge one sorted array into another sorted array.
Note: The size of first array is (m+n) but only first m locations are populated remaining are empty. The second array is of size equal to n.
To merge one sorted array into another sorted array in C, you can implement a straightforward algorithm where you compare elements from both arrays and insert them into a new array in sorted order. This approach ensures that the resulting array remains sorted without requiring additional space beyond the arrays being merged.
.Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h>
// Function to merge two sorted arrays into one sorted array
void merge2arrs(int *bgArr, int bgArrCtr, int *smlArr, int smlArrCtr) {
    // Check for invalid arrays
    if (bgArr == NULL || smlArr == NULL)
        return;
    // Initialize indices for arrays
    int bgArrIndex = bgArrCtr - 1,
        smlArrIndex = smlArrCtr - 1,
        mergedArrayIndex = bgArrCtr + smlArrCtr - 1;
    // Merge the arrays
    while (bgArrIndex >= 0 && smlArrIndex >= 0) {
        if (bgArr[bgArrIndex] >= smlArr[smlArrIndex]) {
            bgArr[mergedArrayIndex] = bgArr[bgArrIndex];
            mergedArrayIndex--;
            bgArrIndex--;
        } else {
            bgArr[mergedArrayIndex] = smlArr[smlArrIndex];
            mergedArrayIndex--;
            smlArrIndex--;
        }
    }
    // Copy remaining elements from the small array
    if (bgArrIndex < 0) {
        while (smlArrIndex >= 0) {
            bgArr[mergedArrayIndex] = smlArr[smlArrIndex];
            mergedArrayIndex--;
            smlArrIndex--;
        }
    } else if (smlArrIndex < 0) {
        // Copy remaining elements from the large array
        while (bgArrIndex >= 0) {
            bgArr[mergedArrayIndex] = bgArr[bgArrIndex];
            mergedArrayIndex--;
            bgArrIndex--;
        }
    }
}
// Main function
int main() {
    int bigArr[13] = {10, 12, 14, 16, 18, 20, 22};
    int smlArr[6] = {11, 13, 15, 17, 19, 21}; 
    int i;
    // Print the large array
    printf("The given Large Array is :  ");
    for (i = 0; i < 7; i++) {
        printf("%d  ", bigArr[i]);
    } 	
    printf("\n");
    // Print the small array
    printf("The given Small Array is :  ");
    for (i = 0; i < 6; i++) {
        printf("%d  ", smlArr[i]);
    } 	
    printf("\n");	
    // Merge the arrays and print the merged array
    merge2arrs(bigArr, 7, smlArr, 6);
    printf("After merging, the new Array is :\n");
    for (i = 0; i < 13; i++) {
        printf("%d ", bigArr[i]);
    }
    return 0;
}
Sample Output:
The given Large Array is : 10 12 14 16 18 20 22 The given Small Array is : 11 13 15 17 19 21 After merged the new Array is : 10 11 12 13 14 15 16 17 18 19 20 21 22
Flowchart: 1
Flowchart: 2
For more Practice: Solve these Related Problems:
- Write a C program to merge two sorted arrays into one sorted array using the two-pointer technique.
 - Write a C program to merge two sorted arrays and remove duplicates from the merged array.
 - Write a C program to merge two sorted arrays in descending order without using extra space.
 - Write a C program to merge two sorted arrays and then perform a binary search on the merged array.
 
Go to:
PREV : Find Pivot Element in Rotated Array.
NEXT : Rotate Array by N Positions.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
