c++ - Sort a vector in which the n first elements have been already sorted? -
consider std::vector
v
of n
elements, , consider n
first elements have been sorted withn < n
, (n-n)/n
small:
is there clever way using stl algorithms sort vector more rapidly complete std::sort(std::begin(v), std::end(v))
?
edit: clarification: (n-n) unsorted elements should inserted @ right position within n first elements sorted.
edit2: bonus question: , how find n ? (which corresponds first unsorted element)
void foo( std::vector<int> & tab, int n ) { std::sort( begin(tab)+n, end(tab)); std::inplace_merge(begin(tab), begin(tab)+n, end(tab)); }
for edit 2
auto = std::adjacent_find(begin(tab), end(tab), std::greater<int>() ); if (it!=end(tab)) { it++; std::sort( it, end(tab)); std::inplace_merge(begin(tab), it, end(tab)); }
Comments
Post a Comment