Complexity of partial_sort vs nth_element
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
std::partial_sort and std::nth_element are both useful when you do not want to sort an entire range, but they optimize for different end results. The important comparison is not just their complexity formulas, but what postcondition each algorithm guarantees after it finishes.
What partial_sort Guarantees
std::partial_sort(first, middle, last) rearranges the range so that:
- the smallest
middle - firstelements are moved into[first, middle) - that prefix is fully sorted
- the remainder is left in unspecified order
Example:
After this runs, the first three elements are the three smallest values in sorted order.
According to cppreference, the complexity is approximately N * log(M) comparisons, where N = last - first and M = middle - first.
What nth_element Guarantees
std::nth_element(first, nth, last) is weaker but cheaper. It rearranges the range so that:
- the element at
nthis the same one that would appear there in a fully sorted range - every element before
nthis not greater than thenthelement - every element after
nthis not less than thenthelement - neither side is fully sorted
Example:
The element at index 3 ends up where it would be in sorted order, but the prefix [first, nth) is only partitioned, not sorted.
Cppreference describes nth_element as O(N) comparisons on average. That average-case linear behavior is why it is a strong choice for selection tasks such as finding the median or top-k cutoff.
The Practical Difference
If you need the smallest k elements in sorted order, partial_sort matches the requirement directly.
If you only need to know which element belongs at position k, or you only need the smallest k elements in arbitrary order, nth_element is usually cheaper.
That is the real tradeoff:
- '
partial_sortdoes more work but gives you a sorted prefix' - '
nth_elementdoes less work but gives you only partitioning around one pivot position'
Top-K Example
Suppose you want the ten smallest values from a huge vector.
Use partial_sort if you plan to display those ten values in order:
Use nth_element if you only need membership in the smallest ten:
After nth_element, the first ten items are the ten smallest, but not necessarily sorted.
If you need them sorted afterward, a common pattern is:
That often gives a good balance: partition in average linear time, then sort only the small prefix.
Choosing by Goal
Choose partial_sort when:
- you want the smallest
kvalues already sorted - '
kis small relative toN' - the sorted prefix is the actual result you need
Choose nth_element when:
- you need the median or another order statistic
- you need a cutoff value, not a sorted prefix
- you want to partition the data cheaply
The complexity formulas matter, but the output guarantees matter more. An algorithm that computes the wrong postcondition efficiently is still the wrong algorithm.
Memory and Stability
Both algorithms work in place and do not promise stability. If equal elements must preserve their original order, neither one is the right tool by itself.
That is worth remembering because "partial sort" sounds close to "stable sort only on part of the data," but that is not what it means.
Common Pitfalls
The biggest mistake is expecting nth_element to sort the prefix. It does not. It only partitions around the chosen position.
Another mistake is using partial_sort when you only need one statistic such as the median. That pays for sorted output you are not going to use.
Developers also sometimes quote both algorithms as if they solved the same problem with different constants. They do not. Their guarantees are different.
Finally, if you use nth_element for top-k work and then consume the first k elements as if they are ordered, the bug may not show up immediately because the prefix can look almost sorted by accident on some inputs.
Summary
- '
partial_sortproduces a sorted prefix of sizekwith approximatelyN * log(k)comparisons.' - '
nth_elementplaces one element at its final sorted position and partitions around it in average linear time.' - Use
partial_sortwhen you need the smallestkvalues already ordered. - Use
nth_elementwhen you need selection or partitioning, not a sorted prefix. - If needed,
nth_elementplussorton the prefix is often a strong top-k strategy.

