Is it possible to sort a list based off one item?
For instance, if I have
1,3,2,4,5,6,7 ... 1000000
And I know that 3 is the second element, is it possible to efficiently sort 3 into it’s correct position between 2 and 4 without re-sorting the entire list?
EDIT: I should also note that, in this scenario, it is assumed that the rest of the list is already sorted; it is simply the 3 that is now out of place.
You could simply find that unordered object (O(n)), take the object out (O(1)), find the correct position (O(n)), then insert it again (O(1)).
Assuming C++11,
Before C++11 you need to implement
std::is_sorted_untilyourself.