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

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

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 (Here, array already sorted so don’t need to this).

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.

 

Note: Both arrays are equal in size so we need to calculate the median using even techniques. Because 2n is always an even number, so the median will be the average of the middle two numbers.

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

C program to find a median of two sorted arrays of the same 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 same size
int findMedianTwoSameSizeSortedArray(int arr1[], int arr2[], int n)
{
    int arr1Index = 0;
    int arr2Index = 0;
    int cnt;
    int median1 = -1, median2 = -1;


    for (cnt = 0; cnt <= n; cnt++)
    {
        if (arr1Index == n)
        {
            //all elements of arr1[] are
            //smaller than arr2[0]
            median1 = median2;
            median2 = arr2[0];
            break;
        }
        else if (arr2Index == n)
        {
            //all elements of arr2[] are
            //smaller than arr1[0]
            median1 = median2;
            median2 = arr1[0];
            break;
        }

        if (arr1[arr1Index] <= arr2[arr2Index])
        {
            //Store the prev median
            median1 = median2;
            median2 = arr1[arr1Index];
            arr1Index++;
        }
        else
        {
            //Store the prev median
            median1 = median2;
            median2 = arr2[arr2Index];
            arr2Index++;
        }
    }

    return (median1 + median2)/2;
}


int main()
{
    int arr1[] = {1, 12, 15, 26, 38};
    int arr2[] = {2, 13, 17, 30, 45};

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

    if (N1 == N2)
    {
        printf("Median is %d", findMedianTwoSameSizeSortedArray(arr1, arr2, N1));
    }
    else
    {
        printf("Doesn't work for arrays of unequal size");
    }

    return 0;
}

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

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *