Median
This algorithm computes the median of the given set of numbers.
void Sort(double* data, int count) {
int min;
for (int i = 0; i < count - 1; ++i)
{
min = i;
for (int j = i + 1; j < count; ++j)
{
if (data[j] < data[min])
{
min = j;
}
}
double temp = data[min];
data[min] = data[i];
data[i] = temp;
}
}
double Median(double data[], int dataSize) {
Sort(data, dataSize);
if (dataSize % 2 == 0)
return (data[dataSize / 2 - 1] + data[dataSize / 2]) / 2;
else
return data[dataSize / 2];
}
Example
double data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double median = Median(data, 10);
Output
5.5000000000000000