Median
This algorithm computes the median of the given set of numbers.
/*****Please include following header files*****/
// vector
// algorithm
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
double Median(vector<double> data) {
sort(data.begin(), data.end());
if (data.size() % 2 == 0)
return (data[data.size() / 2 - 1] + data[data.size() / 2]) / 2;
else
return data[data.size() / 2];
}
Example
vector<double> data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double median = Median(data);
Output
5.5000000000000000