fork(1) download
  1. // A C program to find median of two sorted arrays of unequal size
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. float findMedian( int a[], int n, int b[], int m )
  6. {
  7. int k,i=0,count,j=0;
  8. float m1,m2;
  9. k=(n+m)/2+1;
  10. // printf("%d %d %d ",n,m,k);
  11. for(count=0;count<k;count++){
  12. if(i==n){
  13. m1=m2;
  14. m2=b[j];
  15. j++;
  16. }
  17. else if(j==m){
  18. m1=m2;
  19. m2=a[i];
  20. i++;
  21. }
  22. else if(a[i]<b[j] && i<n){
  23. m1=m2;
  24. m2=a[i];
  25. i++;
  26. }
  27. else if(a[i]>b[j] && j<m){
  28. m1=m2;
  29. m2=b[j];
  30. j++;
  31. }
  32.  
  33. }
  34. if((n+m)%2==0){
  35. return (m1+m2)/2;
  36. }else
  37. return m2;
  38. }
  39.  
  40. // Driver program to test above functions
  41. int main()
  42. {
  43. int A[] = {1,2};
  44. int B[] = {5, 8, 10};
  45.  
  46. int N = sizeof(A) / sizeof(A[0]);
  47. int M = sizeof(B) / sizeof(B[0]);
  48. //printf("%d %d",M,N);
  49.  
  50. printf( "%f", findMedian( A, N, B, M ) );
  51. return 0;
  52. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
5.000000