QtConcurrent: use nth_element to calculate the (correct) median

Sorting is O(NlogN) complexity, while nth_element is linear.
Also remove the errornous +1 when calculating the median position.

Change-Id: Ib39085b59a6c5d15a3a940b1ce3377080340bc09
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2013-09-11 23:10:18 +02:00 committed by The Qt Project
parent 1ec3718432
commit 880b614c8f

View File

@ -102,9 +102,10 @@ public:
{
if (dirty) {
dirty = false;
QVector<T> sorted = values;
std::sort(sorted.begin(), sorted.end());
currentMedian = sorted.at(bufferSize / 2 + 1);
QVector<T> copy = values;
typename QVector<T>::iterator begin = copy.begin(), mid = copy.begin() + bufferSize/2, end = copy.end();
std::nth_element(begin, mid, end);
currentMedian = *mid;
}
return currentMedian;
}