Median
Median is the middle value of a set of data. To determine the median value in a sequence of numbers, the numbers must first be arranged in ascending order.
- If there is an odd amount of numbers, the median value is the number that is in the middle, with the same amount of numbers below and above.
- If there is an even amount of numbers in the list, the median is the average of the two middle values.
Fact about Median :
- Median is joined by the mean and the mode to create a grouping called measures of central tendency.
- Median is an important measure (compared to mean) for distorted data, because median is not so easily distorted. For example, median of {1, 2, 2, 5, 100) is 2 and mean is 22.
- If user add a constant to every value, the mean and median increase by the same constant.
- If user multiply every value by a constant, the mean and the median will also be multiplied by that constant.
Formula of Median of ungrouped data :
Formula of Median of grouped data :
How to find a median of an unsorted array ?
Naive solution:
Given n size unsorted array, find its median.
Median of a sorted array of size n is defined as below :
It is middle element when n is odd and average of middle two elements when n is even. Since the array is not sorted here, we sort the array first, then apply above formula.
Examples:
Input : {1, 3, 4, 2, 6, 5, 8, 7} Output : Median = 4.5 Since number of elements are even, median is average of 4th and 5th largest elements. which means (4 + 5)/2 = 4.5 Input : {4, 4, 4, 4, 4} Output : Median = 4
Below is the code implementation:
C++
// CPP program to find median #include <bits/stdc++.h> using namespace std; // Function for calculating median double findMedian( int a[], int n) { // First we sort the array sort(a, a + n); // check for even case if (n % 2 != 0) return ( double )a[n / 2]; return ( double )(a[(n - 1) / 2] + a[n / 2]) / 2.0; } // Driver program int main() { int a[] = { 1, 3, 4, 2, 7, 5, 8, 6 }; int n = sizeof (a) / sizeof (a[0]); cout << "Median = " << findMedian(a, n) << endl; return 0; } |
Java
// Java program to find median import java.util.*; class GFG { // Function for calculating median public static double findMedian( int a[], int n) { // First we sort the array Arrays.sort(a); // check for even case if (n % 2 != 0 ) return ( double )a[n / 2 ]; return ( double )(a[(n - 1 ) / 2 ] + a[n / 2 ]) / 2.0 ; } // Driver program public static void main(String args[]) { int a[] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 }; int n = a.length; System.out.println( "Median = " + findMedian(a, n)); } } |
Python3
# Python3 program to find median # Function for calculating median def findMedian(a, n): # First we sort the array sorted (a) # check for even case if n % 2 ! = 0 : return float (a[n / / 2 ]) return float ((a[ int ((n - 1 ) / 2 )] + a[ int (n / 2 )]) / 2.0 ) # Driver program a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] n = len (a) print ( "Median =" , findMedian(a, n)) |
C#
// C# program to find median using System; class GFG { // Function for // calculating median public static double findMedian( int [] a, int n) { // First we sort // the array Array.Sort(a); // check for // even case if (n % 2 != 0) return ( double )a[n / 2]; return ( double )(a[(n - 1) / 2] + a[n / 2]) / 2.0; } // Driver Code public static void Main() { int [] a = { 1, 3, 4, 2, 7, 5, 8, 6 }; int n = a.Length; Console.Write( "Median = " + findMedian(a, n) + "\n" ); } } |
PHP
<?php // PHP program to find median // Function for // calculating median function findMedian(& $a , $n ) { // First we sort the array sort( $a ); // check for even case if ( $n % 2 != 0) return (double) $a [ $n / 2]; return (double)( $a [( $n - 1) / 2] + $a [ $n / 2]) / 2.0; } // Driver Code $a = array (1, 3, 4, 2, 7, 5, 8, 6); $n = sizeof( $a ); echo "Median = " . findMedian( $a , $n ); ?> |
Javascript
<script> // JavaScript program to find median // Function for calculating median function findMedian(a, n) { // First we sort the array a.sort(); // check for even case if (n % 2 != 0) return a[parseInt(n / 2)]; return (a[parseInt((n - 1) / 2)] + a[parseInt(n / 2)]) / 2.0; } // Driver program let a = [ 1, 3, 4, 2, 7, 5, 8, 6 ]; let n = a.length; document.write( "Median = " + findMedian(a, n)); </script> |
Output:
Median = 4.5
Time Complexity to find median = O(n Log n) as we need to sort the array first. Note that we can find median in O(n) time using methods discussed here and here.
Basic Program related to Median:
- Maximize the median of an array
- Minimum Increment / decrement to make array elements equal
- Minimum sum of differences with an element in an array
- Median of two sorted arrays of different sizes | Set 1 (Linear)
- Median of two sorted arrays with different sizes in O(log(min(n, m)))
More problems related to Median:
- Median and Mode using Counting Sort
- Minimum number of elements to add to make median equals x
- Decode a median string to the original string
- Median after K additional integers
- Find median in row wise sorted matrix
- Find median of BST in O(n) time and O(1) space
- Median in a stream of integers (running integers)