Median of unsorted array – Java Program for Mean and Median of an Unsorted Array

Median of unsorted array: In the previous article, we have seen Java Program to Find all Elements in Array which have at-least Two Smaller Elements

In this article we are going to see how to find mean and median in an unsorted array using Java programming language.

Java Program for Mean and Median of an Unsorted Array

Find median of unsorted array: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see how to find mean and median in an unsorted array.

Method-1: Java Program for Mean and Median of an Unsorted Array By Static Initialization of Array Elements

Approach:

  • Declare an array with elements.
  • To find mean:
    • Iterate over the array and fid its sum.
    • Return sum/arr.length
  • To find median:
    • Sort the array.
    • If array size is odd, return middle element.
    • If array size is even, return average of middle two elements.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        //array declared and initialized
        int arr[]={1,2,3,4,5,6,7,8};
        //calling findMedian() method
        System.out.println("Median = " + findMedian(arr));
        //calling findMean() method
        System.out.println("Mean = " + findMean(arr));
    }

    //findMedian() user defined method tofind median
    public static double findMedian(int[] arr) 
    {
        // sort the array
        Arrays.sort(arr);
        // if array size is odd, return middle element
        if (arr.length % 2 != 0) 
        {
            return arr[arr.length / 2];
        }
        // if array size is even, return average of middle two elements
        return (double)((arr[arr.length / 2] + arr[(arr.length / 2) - 1]) / 2.0);
    }

    //findMean() user defined method to find mean
    public static double findMean(int[] arr) 
    {
        // find the sum of all the elements
        int sum = 0;
        for (int i : arr) 
        {
            sum += i;
        }
        // return the mean
        return sum / (double)arr.length;
    }

}

Output:

Median = 4.5
Mean = 4.5

Method-2: Java Program for Mean and Median of an Unsorted Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask user for the length of the array.
  • Initialize the array with given size.
  • To find mean:
    • Iterate over the array and fid its sum.
    • Return sum/arr.length
  • To find median:
    • Sort the array.
    • If array size is odd, return middle element.
    • If array size is even, return average of middle two elements.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        //calling findMedian() method
        System.out.println("Median = " + findMedian(arr));
        //calling findMean() method
        System.out.println("Mean = " + findMean(arr));
    }

    //findMedian() user defined method tofind median
    public static double findMedian(int[] arr) 
    {
        // sort the array
        Arrays.sort(arr);
        // if array size is odd, return middle element
        if (arr.length % 2 != 0) 
        {
            return arr[arr.length / 2];
        }
        // if array size is even, return average of middle two elements
        return (double)((arr[arr.length / 2] + arr[(arr.length / 2) - 1]) / 2.0);
    }

    //findMean() user defined method to find mean
    public static double findMean(int[] arr) 
    {
        // find the sum of all the elements
        int sum = 0;
        for (int i : arr) 
        {
            sum += i;
        }
        // return the mean
        return sum / (double)arr.length;
    }

}

Output:

Enter the size of array: 6
Enter array elements: 10 20 30 40 50 60
Median = 35.0
Mean = 35.0

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: