Don't adjust algorithms. The algorithm is clear ("find minimum"). Adjust the search criteria instead, and stay in O(n) realm.
Code.
#include <algorithm>
#include <vector>
#include <iostream>
int main () {
// std::min_element()
std::vector<float> vec;
vec.push_back(0);
vec.push_back(-1);
vec.push_back(-2);
vec.push_back(2);
vec.push_back(4);
auto cmp = [](float lhs, float rhs) {
const bool lz = lhs < 0,
rz = rhs < 0;
if (lz && rz) return lhs < rhs;
if (lz) return false;
if (rz) return true;
return lhs < rhs;
};
const float mp = *std::min_element (vec.begin(), vec.end(), cmp);
std::cout << mp << '\n';
// demonstration of our comparison
sort (vec.begin(), vec.end(), cmp);
for (auto it=vec.begin(), end=vec.end(); it!=end; ++it)
std::cout << *it << " ";
std::cout << std::endl;
}
Output.
0
0 2 4 -1 -2
Explanation.
Our sorting function is encoded in cmp
. It checks the signs of its operands. If both are negative, the bigger one wins. If only the LHS is negative, then RHS is automatically preferred in the sorting. On the opposite, if the RHS is negative, LHS is preferred. Of both are positive, we fall back to the normal order.
Nice thing is that this runs exactly one time over the range and in O(n).