I am using std::nth_element
to get a (roughly correct) value for a percentile of a vector, like so:
double percentile(std::vector<double> &vectorIn, double percent)
{
std::nth_element(vectorIn.begin(), vectorIn.begin() + (percent*vectorIn.size())/100, vectorIn.end());
return vectorIn[(percent*vectorIn.size())/100];
}
I noticed that for vectorIn lengths of up to 32 elements, the vector gets completely sorted. Starting from 33 elements it is never sorted (as expected).
Not sure whether this matters but the function is in a "(Matlab-)mex c++ code" that is compiled via Matlab using the "Microsoft Windows SDK 7.1 (C++)".
EDIT:
Also see the following histogram of the lengths of the longest sorted blocks in 1e5 vectors passed to the function (vectors contained 1e4 random elements and a random percentile was calculated). Note the peak at very small values.