
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Mean and Median of an Unsorted Array in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find mean and median of an unsorted array in Java.
Mean of an array can be derived by calculating the average value of all the elements present inside the array.
Mean= (sum of all elements present in array) / (total number of elements present)
Median of an array represents the middle element present in an odd number sorted array and if the sorted array consists of even number, then median can be found out by calculating the average of middle two numbers.
Let's explore the article to see how it can be done by using Java programming language.
To show you some instances
Instance-1
Given Array= [12, 23, 34, 45, 15].
Mean value of that array= (12 + 23 + 34 + 45 + 15) / (5) = 129 / 5 = 25.8
Sorted array of given array= [12, 15, 24, 34, 45]
As this is an odd numbered array the median is the middle element.
Median = 24
Instance-2
Given Array= [38, 94, 86, 63, 36].
Mean value of that array= (38 + 94 + 86 + 63 + 36) / (5) = 317 / 5 = 63.4
Sorted array of given array= [36, 38, 63, 86, 94]
As this is an odd numbered array the median is the middle element.
Median = 63
Instance-3
Given Array= [54, 67, 23, 95, 24, 60].
Mean value of that array= (54 + 67 + 23 + 95 + 24 + 60) / (6) = 323 / 6 = 53.83
As this is an even numbered array the median is the average value of middle two elements.
Sorted array of given array= [23, 24, 54, 60, 67, 95]
Median = (54 + 60) / 2 = 57
Algorithm
Step 1 ? Declare and initialize an array of integer type.
Step 2 ? Sort the array in ascending order.
Step 3 ? In first user- defined method we find mean value. And in second user- defined method we find the median value.
Step 4 ? Call both user-defined method and pass the array and the length value as parameters.
Step 5 ? After finding the mean and median values print both the values as output.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it ?
array.length
where, ?array' refers to the array reference.
You can use Arrays.sort() method to sort the array in ascending order.
Arrays.sort(array_name);
Multiple Approaches
We have provided the solution in different approaches
By Using Static Input Method
By Using User Input Method
Let's see the program along with its output one by one.
Approach-1: By Using Static Input Method
In this approach, we declare an array by static input method and pass this array and its length as parameter in our user defined method, then inside the method by using the algorithm we can find the mean and median values.
Example
import java.util.*; public class Main { public static void main(String args[]) { //declare an integer type array and store some random integer values int inputArray[] = { 23, 24, 65, 87, 85, 12, 76,21}; //declare an integer variable to store the length of the given array int len = inputArray.length; //call the user defined method and print the output System.out.println("Mean of given array "+ Arrays.toString(inputArray)+ " is = " + mean(inputArray, len)); System.out.println("Median of given array "+ Arrays.toString(inputArray) + " is = " + median(inputArray, len)); } // user defined function to find mean value public static double mean(int arr[], int len) { //declare a Integer variable to store the sum value of given array's elements int sum = 0; //initiate the loop to calculate the sum value of all the elements of given array for (int i = 0; i < len; i++) sum += arr[i]; //return the mean value return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0; } //user defined function to find median value public static double median(int arr[], int len) { // sort the given array in ascending order Arrays.sort(arr); // check whether the given array consists of even or odd number of elements if (len % 2 != 0) { return (double)arr[len / 2]; } return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0; } }
Output
Mean of given array [23, 24, 65, 87, 85, 12, 76, 21] is = 49.125 Median of given array [23, 24, 65, 87, 85, 12, 76, 21] is = 44.5
Approach-2: By Using User Input Method
In this approach, we declare an array by user input method and pass this array and its length as parameter in our user defined method, then inside the method by using the algorithm we can find the mean and median values.
Example
import java.util.*; public class Main { public static void main(String args[]) { //create the Objectof Scanner class Scanner sc=new Scanner(System.in); //ask the user to enter the length of the array System.out.print("Enter the number of elements: "); //declare a variable to store the length int len=sc.nextInt(); //declare an empty array of given length int[] inputArray = new int[len]; System.out.println("Enter the elements: "); //initiate the loop to store the elements into the array for(int i=0; i < len; i++) { //store the elements intop the array inputArray[i]=sc.nextInt(); } //call the user defined method and print the output System.out.println("Mean of given array "+ Arrays.toString(inputArray) + " is = " + mean(inputArray, len)); System.out.println("Median of given array "+ Arrays.toString(inputArray) + " is = " + median(inputArray, len)); } // user defined function to find mean value public static double mean(int arr[], int len) { //declare a Integer variable to store the sum value of given array's elements int sum = 0; //initiate the loop to calculate the sum value of all the elements of given array for (int i = 0; i < len; i++) sum += arr[i]; //return the mean value return (double)sum / (double)len; } //user defined function to find median value public static double median(int arr[], int len) { // sort the given array in ascending order Arrays.sort(arr); // check whether the given array consists of even or odd number of elements if (len % 2 != 0){ return (double)arr[len / 2]; } System.out.println(arr[(len - 1)]); System.out.println(arr[(len - 1)]); return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0; } }
Output
Enter the number of elements: 8 Enter the elements: 2 5 3 7 1 6 8 4 Mean of given array [2, 5, 3, 7, 1, 6, 8, 4] is = 4.5 Median of given array [2, 5, 3, 7, 1, 6, 8, 4] is = 4.5
In this article, we explored how to find mean and median of an array in an unsorted array by using Java programming language.