AK: Make Statistics::median return a mean for an even number of values

The previous implementation of Statistics::median() was slightly
incorrect with an even number of elements since in those cases it needs
to be the arithmetic mean of the two elements that share the middle
position.
This commit is contained in:
Staubfinger 2023-01-06 21:38:21 +01:00 committed by Jelle Raaijmakers
parent 0cede94c39
commit 25dd0a4d2d
Notes: sideshowbarker 2024-07-17 05:23:40 +09:00

View File

@ -55,6 +55,11 @@ public:
T const median()
{
quick_sort(m_values);
// If the number of values is even, the median is the arithmetic mean of the two middle values
if (size() % 2 == 0) {
auto index = size() / 2;
return (m_values.at(index) + m_values.at(index + 1)) / 2;
}
return m_values.at(size() / 2);
}