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; }
Recommended Articles for you:
- Best gift for programmers.
- Best electronic kits for programmers.
- C Program to find the length of longest consecutive elements sequence from the given unsorted array of integers.
- C Program to find first and last position of element in sorted array
- Write C program to find the missing number in a given integer array of 1 to n
- C program to find the most popular element in an array
- Find the largest and smallest element in an array using C programming.
- C program to find even occurring elements in an array of limited range
- Find sum of all sub-array of a given array.
- C program to segregate even and odd numbers
- Find an element in array such that sum of left array is equal to sum of right array.
- C Program to find the count of even and odd elements in the array.
- Write C program to find the sum of array elements.
- C program to find odd occurring elements in an array of limited range
- Find the sum of array elements using recursion
- C Program to reverse the elements of an array
- C Program to find the maximum and minimum element in the array
- Calculate size of an array in without using sizeof in C
- How to create a dynamic array in C?
- How to access 2d array in C?
- Dangling, Void, Null and Wild Pointers