C program to find median of two sorted arrays of different sizes

In this blog post, we learn how to write a C program to find the median of two sorted arrays of different sizes? So here we will write the C program to find the median of two sorted arrays of different sizes. We will also see how to display the median of two given sorted arrays arr1 and arr2 of size N1 and N2 using C programming.

Example,

Input: 

arr1[] = {-5, 3, 6, 12, 15};
arr2[] = {-12, -10, -6, -3, 4, 10};


Output: The median is 3.


Explanation:

The merged array is :
ar3[] = {-12, -10, -6, -5 , -3,
          3, 4, 6, 10, 12, 15};
		  
So the median of the merged array is 3

 

But before starting the programming I want to explain the median. Basically median is an element that divides the array into two parts left and right.  So let’s see how we can find the median of an array.

1. Arrange the array elements from smallest to largest.

2. If the number of elements in the array is odd, the median is the middle element in the list.

Example,

//Given input integer sorted array
Input: arr[] = {0, 1, 2, 3, 4};

Output: 2

Explanation: There is an odd number of elements (5), 
so the median is the middle element that is 2

In general, for an array of n elements, where n is an odd number median will be:

ArrayMedian =>  (n+1)/2  

3. If the number of elements in the array is even, the median is the average of the middle two elements.

Example,

//Given input integer sorted array
Input: arr[] = {0, 1, 2, 3};

Output: (1+2)/2 = > 1.5

Explanation: The median is the mean of the middle two numbers 1 and 2.

In general, for an array of n elements, where n is an even number median will be:

ArrayMedian =  (m1+m2)/2 element, where m1 and m2 two middle number.

If you want to learn more about the C language, you can check this course, Free Trial Available.

C program to find median of two sorted arrays of different size

#include <stdio.h>
#include <stdlib.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


//return median of two sorted array of different size
int findMedianTwoSortedArray(int arr1[], int arr2[], int N1, int N2)
{
    int arr1Index = 0;
    int arr2Index = 0;
    int cnt = 0;
    int median1 = -1, median2 = -1, medianOfArr1Arr2;

    //if (N1+N2) is odd
    if((N2 + N1) % 2 == 1)
    {
        for (cnt = 0; cnt <= (N1 + N2)/2; cnt++)
        {
            if(arr1Index != N1 && arr2Index != N2)
            {
                median1 = (arr1[arr1Index] > arr2[arr2Index]) ? arr2[arr2Index++] : arr1[arr1Index++];
            }
            else if(arr1Index < N1)
            {
                median1 = arr1[arr1Index++];
            }
            // for case when arr2Index < N2,
            else
            {
                median1 = arr2[arr2Index++];
            }
        }
        medianOfArr1Arr2 = median1;
    }
    else
    {
        //if N1+N2 is even
        for (cnt = 0; cnt <= (N1 + N2)/2; cnt++)
        {
            median2 = median1;
            if(arr1Index != N1 && arr2Index != N2)
            {
                median1 = (arr1[arr1Index] > arr2[arr2Index]) ? arr2[arr2Index++] : arr1[arr1Index++];
            }
            else if(arr1Index < N1)
            {
                median1 = arr1[arr1Index++];
            }
            // for case when arr2Index < N2,
            else
            {
                median1 = arr1[arr2Index++];
            }
        }
        medianOfArr1Arr2 = (median1 + median2)/2;
    }

    return medianOfArr1Arr2;
}


int main()
{
    int arr1[] = {-5, 3, 6, 12, 15};
    int arr2[] = {-12, -10, -6, -3, 4, 10};

    const int N1 = ARRAY_SIZE(arr1);
    const int N2 = ARRAY_SIZE(arr2);

    printf("Median is %d", findMedianTwoSortedArray(arr1, arr2, N1, N2));


    return 0;
}

C program to find median of two sorted arrays of different size