I need to sort some arrays of floats, modify the values, and then construct an array with the original ordering, but the modified values.
In R, I could use the rank() and order() functions to achieve this:
v a vector
v[order(v)] is sorted
v[i] goes in the rank(v)th spot in the sorted vector
Is there some equivalent of these functions in the standard c or c++ libraries? A permutation matrix or other way of encoding the same information would be fine too.
O(n) space and O(nlogn) time would be ideal.
There is the equivalent to the
rankfunction in C++: it’s called nth_element and can be applied on any model of Random Access Container (among whichvectoranddequeare prominent).Now, the issue seems, to me, that the operate on values might actually modify the values and thus the ranks would change. Therefore I would advise storing the ranks.
std::vector<float>tostd::vector< std::pair<float, rank_t> >vector(works without any predicate)std::vector< std::pair<float, rank_t> >tostd::vector<float>Unless of course you want
nth_elementto be affected by the current modifications of the values that occurred.