Quantcast
Channel: C Programming Archives - QnA Plus
Viewing all articles
Browse latest Browse all 93

C Program to Merge Two Sorted Arrays into one Sorted Array

$
0
0

Here we’ll write a C function to merge two sorted arrays into one. The merged array will also be sorted. Here we’ll use two sorted integer arrays that are sorted in ascending order. After merging the result array will contain all the numbers from the original arrays and will be sorted in ascending order. The logic will be similar for descending arrays.

We use this technique in conquer phase of merge sort.

int merge(int *arr1, int len1, int *arr2, int len2, int *arr) {
    int i = 0, j = 0, k = 0;
    while((j < len1) && (k < len2)) {
        arr[i++] = (arr1[j] < arr2[k])?arr1[j++]:arr2[k++];
    }

    while(j < len1) arr[i++] = arr1[j++];
    while(k < len2) arr[i++] = arr2[k++];
    
    return i;
}

Input parameters of this function:
arr1: First sorted array
len1: Length of the first array.
arr2: Second sorted array.
len2: Length of the second array.
arr: Output result array.

This function returns the length of the merged array which basically len1 plus len2.

We maintain three index variables i, j and k for three arrays, two input arrays (arr1 and arr2) and the output array (arr). The first while loop continues until all elements of one of the input arrays are copied into the result array (arr). In every iteration, if arr1[j] is smaller than arr2[k], then arr1[j] is copied to arr[i] and i and j are incremented by 1. Similarly, if arr2[k] is smaller than arr1[j] then arr2[k] is copied to arr[i] and i and j are incremented by 1.

After completion of the first while loop all numbers from one of the two arrays are copied to the result array. The next two while loops copy the remaining numbers of the other array to the result array.

The Complete Program.

Here is the complete program.

#include <stdio.h>

int merge(int *arr1, int len1, int *arr2, int len2, int *arr) {
    int i = 0, j = 0, k = 0;
    while((j < len1) && (k < len2)) {
        arr[i++] = (arr1[j] < arr2[k])?arr1[j++]:arr2[k++];
    }

    while(j < len1) arr[i++] = arr1[j++];
    while(k < len2) arr[i++] = arr2[k++];
    
    return i;
}

void print_arr(int *arr, int len) {
    int i = 0;
    while(i < len) {
        printf("%d ", arr[i++]);
    }
    printf("\n");
}

int main() {
    int arr1[] = {3, 5, 7, 10, 15, 19, 21, 34, 67};
    int arr2[] = {1, 4, 7, 9, 18, 26, 30, 56, 71, 81, 94};
    int arr[32] = {0};
    int len = 0;
    int l1 = sizeof(arr1) / sizeof(int);
    int l2 = sizeof(arr2) / sizeof(int);
    
    printf("First Array: ");
    print_arr(arr1, l1);
    printf("Second Array: ");
    print_arr(arr2, l2);

    len = merge(arr1, l1, arr2, l2, arr);
    printf("Merged Array: ");
    print_arr(arr, len);
    return 0;
}

Here is the output of the program.

First Array: 3 5 7 10 15 19 21 34 67
Second Array: 1 4 7 9 18 26 30 56 71 81 94
Merged Array: 1 3 4 5 7 7 9 10 15 18 19 21 26 30 34 56 67 71 81 94

The post C Program to Merge Two Sorted Arrays into one Sorted Array appeared first on QnA Plus.


Viewing all articles
Browse latest Browse all 93

Trending Articles