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:

sorted/unsorted vector

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

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -