Maths Algorithms in C#: The Median
The median is defined as the data point that falls exactly at the midpoint of a set of data points. If there is an even number of points, then the average is taken of the middle two points.
There is one consideration though – the data points sent through to this function needs to be sorted, otherwise it will return garbage data.
public static double Median(List<double> values)
{
int mid;
double median;
values.Sort();
if (MathExt.IsEven(values.Count))
{
mid = (values.Count / 2) - 1;
median = (values[mid] + values[mid + 1]) / 2.0;
}
else
{
mid = (values.Count / 2);
median = values[mid];
}
return median;
}
The full sourcecode for the MathLib library is available at https://github.com/sjmeunier/mathlib
Originally posted on my old blog, Smoky Cogs, on 23 Oct 2009
Updated 5 Oct 2016: Updated code snippet after refactoring MathLib library