w3resource

C Exercises: Merge one sorted array into another sorted array

C Array: Exercise-38 with Solution

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:

C Exercises: Merge one sorted array into another sorted array

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: Merge one sorted array into another sorted array.

Flowchart: 2

Flowchart: Merge one sorted array into another sorted array.

C Programming Code Editor:

Previous: Write a program in C to find the pivot element of a sorted and rotated array using binary search.
Next: Write a program in C to rotate an array by N positions.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/array/c-array-exercise-38.php