Sort a vector in which the n first elements have been already sorted?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Knowing that the first n elements of a vector are already sorted sounds useful, but whether it helps depends on what you know about the remaining elements. If the tail is arbitrary and unsorted, there is no general magical shortcut that beats full sorting asymptotically for all comparison-based inputs, though certain practical strategies can exploit the sorted prefix in specific cases.
The First Question To Ask
You need to distinguish between these situations:
- the first
nelements are sorted and the rest are arbitrary - the first
nelements are sorted and the rest are also sorted separately - the unsorted suffix is very small
- each new element is appended one at a time to an already sorted prefix
These cases have different best solutions.
If The Suffix Is Arbitrary
If elements after position n - 1 are completely unsorted, the safe general answer is usually to sort the whole vector with the standard library.
Why is this often still correct? Because the unsorted suffix can contain any values, including values smaller than the already sorted prefix. That destroys the simple merge structure.
If You Append One Element At A Time
If the vector is sorted except for one new appended element, insertion is better than re-sorting everything.
This works because only one value is out of place. The sorted prefix gives you a binary-search insertion point.
If The Tail Is Also Sorted
If the vector really contains two sorted runs, then merging is the right tool.
This is much faster than a general full sort because the structure is stronger: both halves are already sorted.
If The Unsorted Tail Is Small
When the unsorted suffix is tiny, insertion-based strategies can be practical even if the suffix itself is not internally sorted. You can process each tail element and insert it into the prefix.
This is useful when the tail length is small enough that repeated insertion costs are acceptable.
The Algorithmic Reality
The phrase "first n elements are sorted" alone does not imply a globally easier problem. Without more structure, the unsorted suffix may still force almost full sorting work.
That is why standard sorting algorithms such as Timsort or introsort remain strong defaults. They already exploit existing order when they detect it, so using the standard library is often both simpler and fast.
Common Pitfalls
The most common mistake is assuming a sorted prefix always implies a merge problem. It does not unless the remaining suffix is also sorted or otherwise constrained.
Another mistake is hand-writing a complex partial-sort algorithm when std::sort already performs well and is easier to trust.
A third issue is ignoring the distribution of the unsorted elements. A tiny suffix and a half-unsorted vector are very different engineering situations.
Summary
- A sorted prefix only helps if you know something useful about the remaining elements.
- If the suffix is arbitrary, full sorting is often still the right general solution.
- If only one appended value is out of place, binary-search insertion works well.
- If both parts are sorted, use merge or
std::inplace_merge. - Choose the algorithm based on the actual structure, not on the prefix fact alone.
Related reading
- Sort Algorithm - find which chart bar sees different bar
- Sort algorithm for Excel / SharedStrings
- Sort an array according to the elements of another array
- Sort an array in Java
- Sort an array of integers into odd, then even
- Sort array by firstname (alphabetically) in JavaScript
- Sort array in the minimum number of moves
- Sort BST in On using constant memory

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.