I want to repeatedly rearrange an array or std::vector
so that the minimum is the first element, the maximum is the last element, and arr[(0+lastIdx)/2]
would be the median, elements before the median is less than median, elements after the median would be greater. After each time I query for the min, max and median, I will make changes to the data and I want to quickly query those three values again.
Every time I want to rearrange the array, the array is a different array with same size.
Using std::nth_element
I can get the median in the right place, and then I could iterate the array to get min and max. For a single array, this achieves O(n) complexity, and clearly this cannot be improved upon. (Except perhaps, the constant of complexity in front of O(n))
I need to operate on an array, firstly, I rearrange the array, and then do something else, this would make the arranged array totally unarranged again, but no new values are inserted. then, I repeat this procedure over and over again.