Write a C program to find the median of the given two sorted arrays. Median is the middle number when the numbers are sorted in either ascending or descending order.
To implement this, our required inputs are the sizes of two arrays along with the elements or data values of the two arrays. Our desired output is the double type number representing the median of the given two input arrays when combined into one sorted array.
Find Median of Two Sorted Arrays Using Function
The first step here is, to read our necessary inputs and to do so, we can make use of the predefined function scanf() which reads the input of any primitive datatype like int, char, float, double, long, etc., at runtime.
Based on the format specifier given, the datatype of the input to be read is determined. In our given problem statement, all the necessary inputs are of integer type and therefore, we use the ‘%d’ format specifier to read them.
Firstly, we’ll read the sizes of the two arrays (n1, n2) following which we’ll create the two arrays (a1, a2) of the respective size. Then, we’ll read the elements or data values of the two arrays.
printf(“\nEnter the size of Array 1 : “);
scanf(“%d”,&n1);
printf(“\nEnter the size of Array 2 : “);
scanf(“%d”,&n2);
int a1[n1],a2[n2];
printf(“\nEnter the %d sorted elements in Array1:\n”,n1);
for(i=0;i<n1;i++) {
scanf(“%d”,&a1[i]);
}
printf(“\nEnter the %d sorted elements in Array2:\n”,n2);
for(i=0;i<n2;i++) {
scanf(“%d”,&a2[i]);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> #include <stdlib.h> double median(int a1[],int a2[],int n1,int n2) { int i=0,j=0,k=0,a3[n1+n2]; double median; while(i<n1&&j<n2) { if(a1[i]<a2[j]) { a3[k++] = a1[i++]; } else { a3[k++] = a2[j++]; } } while(i < n1) { a3[k++] = a1[i++]; } while(j < n2) { a3[k++] = a2[j++]; } if((n1+n2)%2==1) median=a3[(n1+n2)/2]; else median=(a3[(n1+n2-1)/2]+a3[(n1+n2)/2])/2.0; return median; } int main() { int n1,n2,i,j; printf("\nEnter the size of Array 1 : "); scanf("%d",&n1); printf("\nEnter the size of Array 2 : "); scanf("%d",&n2); int a1[n1],a2[n2]; printf("\nEnter the %d sorted elements in Array1:\n",n1); for(i=0;i<n1;i++) { scanf("%d",&a1[i]); } printf("\nEnter the %d sorted elements in Array2:\n",n2); for(i=0;i<n2;i++) { scanf("%d",&a2[i]); } printf("\nThe Median of two Sorted Arrays is : "); printf("%.2f ",median(a1,a2,n1,n2)); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter the size of Array 1 : 5 Enter the size of Array 2 : 5 Enter the 5 sorted elements in Array1: 10 20 30 40 50 Enter the 5 sorted elements in Array2: 15 25 35 45 55 The Median of two Sorted Arrays is : 32.50 |
Standard Method
In the above method, we have seen the logic to determine the median of the given two sorted arrays using functions. The benefits of using functions is that, it makes the parts of code within functions reusable and also enhances the readability of the code.
If some logic is needed elsewhere in the code and is within a function block of its own then, instead of rewriting the same set of statements we could just make a function call and pass the necessary arguments thereby, making it reusable.
Also, since the code is placed within different function blocks based on its logic and functionality, the code is lot cleaner and is placed systematically.
This way, if some part is to be found we can directly go to the corresponding function block and look for it. Hence, it increases the readability of the code.
As we can observe the same above, the main method is handling all the input/output related operations whereas the user-defined function (median) is handling the logic behind merging the two sorted array and thereby, finding the median.
But, we can also write the entire code within main method as the time and space complexity is the same for both because of the same logic being used.
So, if we are sure that, we do not need to reuse this logic then, there is no harm in writing it within main itself else, we might have to rewrite same set of statements again. We can see below the illustration of the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h> #include <stdlib.h> int main() { int n1,n2,i=0,j=0,k=0,median; printf("\nEnter the size of Array 1 : "); scanf("%d",&n1); printf("\nEnter the size of Array 2 : "); scanf("%d",&n2); int a1[n1],a2[n2],a3[n1+n2]; printf("\nEnter the %d sorted elements in Array1:\n",n1); for(i=0;i<n1;i++) { scanf("%d",&a1[i]); } printf("\nEnter the %d sorted elements in Array2:\n",n2); for(i=0;i<n2;i++) { scanf("%d",&a2[i]); } i=0; while(i<n1&&j<n2) { if(a1[i]<a2[j]) { a3[k++] = a1[i++]; } else { a3[k++] = a2[j++]; } } while(i < n1) { a3[k++] = a1[i++]; } while(j < n2) { a3[k++] = a2[j++]; } if((n1+n2)%2==1) median=a3[(n1+n2)/2]; else median=(a3[(n1+n2-1)/2]+a3[(n1+n2)/2])/2; printf("\nThe Median of two Sorted Arrays is : "); printf("%d ",median); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter the size of Array 1 : 5 Enter the size of Array 2 : 5 Enter the 5 sorted elements in Array1: 1 12 15 26 38 Enter the 5 sorted elements in Array2: 2 13 17 30 45 The Median of two Sorted Arrays is : 16 |