In this short article, we will see how to compute median in Java.
- What is median?
- What is the difference between the median and mean?
- Calculate median with simple algorithm
- Conclusion
What is median?
First of all, let’s get a good idea what median actually is. Let’s look at wiki definition:
The median of a list of numbers can be found by first sorting the numbers ascending. If there is an odd number of values, the middle one is picked. If there is an even number of values, the median is then defined to be the average of the two middle values. (Wikipedia)
So when we take the definition of median, we need to find the middle in the sorted array.
What is the difference between the median and mean?
The essential difference is that the mean gives a rough average (sum all elements together and divide the sum by the number of elements in the collection) of the whole dataset. At the same time,
It is important to note that a single centric value can be picked only from ordered values arranged in an ascending or descending order for the median.
For example, if I have an array [1,2,3,4,5]
, number 3 is the median. But if have an arrray [1,2,3,4,5,6]
, number 3.5 is the median.
Calculate median with simple algorithm
Let’s use few words to describe our algorithm for computing the median.
First, we will need to take input. The inputs are the int
numbers in the array and the size of the collection. One crucial thing to remember is to always keep in mind that the values in an array need to be sorted for computing median Order, ascending or descending, is necessary because we are looking for the value in the centre of the array.
Sorting array happens before computing median, and it is not part of the algorithm for computing median. Therefore, you can use any known sorting algorithm such as quicksort, bubble sort etc.
After taking the inputs, we must first check whether the number of elements in the array is odd or even.
If the number of elements in the array is even, we compute the median by taking two central pieces and divide them by two.
As we are familiar with arrays in computer science, array order starts with zero and ends with the number of elements minus one. Therefore, when an array length is an even number, we must select the middle array element and element before it.
Eventually, for computing the median for even array length, a division with a number represented as a double value will secure that median with decimal points. Otherwise, division with the number represented as int will floor the number, and the median will be incorrect.
Otherwise, if the number of elements in the array is odd, we compute the median by taking the centric array element.
But, again, as we are familiar with arrays in computer science, array order starts with zero and ends with the number of elements minus one.
We will do a small trick, which can look unnatural at first sight. We add one to the number of array elements in the array of odd length, then divide it by two. And then, we subtract from division one. The resulting number will give us the index position of the central component of the array of odd lengths.
public class Median {
public static void main(String arg[]) {
int oddLength = 5;
int oddArr[] = new int[oddLength];
oddArr[0] = 10;
oddArr[1] = 30;
oddArr[2] = 50;
oddArr[3] = 70;
oddArr[4] = 90;
System.out.println("Median for odd length array is : " + computeMedian(oddArr, oddLength);
int evenLength = 6;
int evenArr[] = new int[evenLength];
evenArr[0] = 10;
evenArr[1] = 30;
evenArr[2] = 50;
evenArr[3] = 70;
evenArr[4] = 90;
evenArr[5] = 100;
System.out.println("Median for even length array is :" + computeMedian(evenArr, evenLength);
}
private static double computeMedian(int[] arr, int n) {
if (n % 2 == 0) {
return (arr[n/2 - 1] + arr[n/2]) / 2.0;
} else {
return arr[(n+1)/2 - 1];
}
}
}
Output
Median for odd length array is : 50
Median for even length array is : 60
Conclusion
This article has shown us a simple algorithm for how to compute median in Java.
It is important to remember that we can compute the median only on sorted arrays. Ascending or descending order does not matter unless we are looking for the value in the centre of the array.
Sorting of the array can happen before computing the median, and you can use any known sorting algorithm.
Did you find computing median easy? Do you have your trick or know another way how to calculate median in Java? Let us know in the comments below the article. We would like to hear your ideas and stories.