Find the Median of an Array Java

Find the median of an Array Java | In the sequence of numbers, the middle numbers are termed median, while the numbers are sorted in descending or ascending order.  In this post, we will discuss how to find the median of an array in java. To understand this problem in more detail refer to the below example:-

To find the middlemost element we use logic:-

If the length of the array is odd then,
Median = array [ (n+1) / 2 – 1 ]

If the length of the array is even then,
Median = ( array [n/2 – 1] + array [ n/2 ] ) / 2

Example-1:-
Array = 1,2,3,4,5
Median = 3

As there are odd numbers in the given array. Therefore finding out the median is easy as the array gets divided easily. The number 3 is in the middle, & there are two numbers on both sides of the 3. From the above formula median = array [(5+1) / 2 -1] = array[2], Hence the median = 3.

Example-2:-
Array = 1,2,3,4
Median = 2.5

The above array contains an even number of elements, therefore median = ( array[4/2-1] + array[4/2] ) / 2 = (2 + 3)/2 = 2.5. Hence the median of the given array = 2.5

Java Program to Find Median of an Array by Standard Method

In the below program, we have initialized an array with 0, 1, 2, 3, and 4 numbers. And used the above formula to find the median of an array. Here the number of array elements is odd.

public class Main {
   public static void main(String arg[]) {
      int arr[] = { 0, 1, 2, 3, 4 };
      int median = 0;
      int n = arr.length;

      if (n % 2 == 1) {
         median = arr[(n + 1) / 2 - 1];
      } else {
         median = (arr[n / 2 - 1] + arr[n / 2]) / 2;
      }
      System.out.println("Median: " + median);
   }
}

Output:-

Median: 2

Find the Median of an Array Java by taking User Input

import java.util.Scanner;

public class Main {

   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter number of array elements: ");
      int n = scan.nextInt();
      double[] input = new double[n];
      System.out.println("Enter " + n + " array elements: ");
      for (int i = 0; i < n; i++) {
         input[i] = scan.nextDouble();
      }
      double middle = median(n, input);
      System.out.println("Median: " + middle);
      scan.close();
   }

   public static double median(int n, double array[]) {
      double middle = 0;
      if (n % 2 == 1) {
         middle = array[((n + 1) / 2) - 1];
      } else {
         middle = (array[n / 2 - 1] + array[n / 2]) / 2;
      }
      return middle;
   }
}

Output:-

Enter number of array elements: 5
Enter 5 array elements:
20 15 30 40 60
Median: 30.0

Enter number of array elements: 6
Enter 6 array elements:
20 40 15 30 56 89
Median: 22.5

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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