Java Program to Find the Median of two Sorted Arrays using Binary Search Approach

«
»
This is a Java Program to find median of two sorted arrays using binary search approach. In probability theory and statistics, a median is described as the number separating the higher half of a sample, a population, or a probability distribution, from the lower half. The median of a finite list of numbers can be found by arranging all the numbers from lowest value to highest value and picking the middle one. However size of both arrays must be equal. The time complexity of the following program is O (log n).

Here is the source code of the Java program to find median of two sorted arrays. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Java Program to Find the Median of two Sorted arrays using 
  3.  * Binary Search Approach
  4.  */
  5.  
  6. import java.util.Scanner;
  7.  
  8. public class MedianOfTwoSortedArrays
  9. {
  10.     public static void main(String[] args) 
  11.     {
  12.         Scanner scan = new Scanner(System.in);
  13.         System.out.println("Enter number of elements in arrays");
  14.         int N = scan.nextInt();
  15.         int[] arr1 = new int[ N ];
  16.         int[] arr2 = new int[ N ];
  17.         System.out.println("Enter "+ N +" elements of array 1");
  18.         for (int i = 0; i < N; i++)
  19.             arr1[i] = scan.nextInt();
  20.         System.out.println("Enter "+ N +" elements of array 2");
  21.         for (int i = 0; i < N; i++)
  22.             arr2[i] = scan.nextInt();
  23.         int med = median(arr1, arr2);
  24.         System.out.println("Median = "+ med);
  25.      }
  26.      public static int median(int[] arr1, int[] arr2)
  27.      {
  28.          int N = arr1.length;
  29.          return median(arr1, 0, N -1 , arr2, 0, N - 1);
  30.      }
  31.      public static int median(int[] arr1, int l1, int h1, int[] arr2, int l2, int h2)
  32.      {
  33.          int mid1 = (h1 + l1 ) / 2;
  34.          int mid2 = (h2 + l2 ) / 2;
  35.  
  36.          if (h1 - l1 == 1)
  37.              return (Math.max(arr1[l1] , arr2[l2]) + Math.min(arr1[h1] , arr2[h2]))/2;
  38.          else if (arr1[mid1] > arr2[mid2])
  39.              return median(arr1, l1, mid1 , arr2, mid2 , h2);    
  40.          else
  41.              return median(arr1, mid1 , h1, arr2, l2 , mid2 );    
  42.      }     
  43. }

advertisement
 
Enter number of elements in arrays
5
Enter 5 elements of array 1
1 12 15 26 38
Enter 5 elements of array 2
2 13 17 30 45
Median = 16

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.