Find the first few lows easy. However, we must find the nth minimum.
If you need to get them sequentially, you can apply a solution that advises @BuilderC : pass the value obtained in the previous step to the function and ignore all elements smaller than this value.
Here is an example of such a code:
#include <iostream> #include <vector> using namespace std; template <typename T> T min_limited(T begin, T end, T ignore) { // Π€ΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½Π΅Ρ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° Π½Π°ΠΈΠΌΠ΅Π½ΡΡΠΈΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΌΠ°ΡΡΠΈΠ²Π°, // Π±ΠΎΠ»ΡΡΠΈΠΉ `ignore`, ΠΈΠ»ΠΈ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π·Π° ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΌΠ°ΡΡΠΈΠ²Π°. T min = end; for (; begin != end; ++begin) if (*begin > *ignore && (min == end || *begin < *min)) min = begin; return min; } int main() { vector<int> V = {1, 5, 2, 5, 6, 3}; auto min_val = min(V.begin(), V.end()); for (int i = 0; min_val != V.end(); ++i) { cout << "Min #" << i << ": " << *min_val << endl; min_val = min_limited(V.begin(), V.end(), min_val); } return 0; }
If you do not know the previous minimum, the task becomes more complicated. In fact, you need to find the k th order statistics (this is the k th minimum), what the median medians (BFPRT) algorithm does .