C++ std::nth_element() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::nth_element() rearranges a range so that the element at position nth is the one that would be there if the whole range were sorted. Everything before it is not greater, and everything after it is not smaller.

It is faster than a full sort and is perfect for finding a median or the k-th smallest value.

Syntax

Add #include <algorithm>, then call std::nth_element(). Start with these two lines.

C++
std::nth_element(v.begin(), v.begin() + 2, v.end());
std::nth_element(v.begin(), v.begin() + 2, v.end(), std::greater<int>());
CallWhat it does
std::nth_element(v.begin(), v.begin() + 2, v.end());Find the third smallest element.
std::nth_element(v.begin(), v.begin() + 2, v.end(), std::greater<int>());Split the top three from the rest.
Library declarations
C++
template<class RandomIt>
void nth_element(RandomIt first, RandomIt nth, RandomIt last);

template<class RandomIt, class Compare>
void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to rearrange.
nthRequired. The position that must end up holding the right element.
compOptional. A comparison function.

Return Value

Return ValueDescription
voidNothing is returned. Read the result at *nth.

C++ Examples

Example 1 — Find the median

Get the middle value of five numbers.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {7, 2, 9, 4, 5};
    auto mid = v.begin() + v.size() / 2;

    std::nth_element(v.begin(), mid, v.end());

    std::cout << "Median: " << *mid << '\n';
    return 0;
}

How It Works

  1. The vector has five elements, so the middle index is 2.
  2. After nth_element(), the element at index 2 is what a full sort would put there.
  3. A sorted copy would be 2 4 5 7 9, and the middle is 5.

Example 2 — Third smallest

Find the k-th smallest number.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {10, 3, 8, 1, 6, 4};

    std::nth_element(v.begin(), v.begin() + 2, v.end());

    std::cout << "Third smallest: " << v[2] << '\n';
    return 0;
}

How It Works

  1. Index 2 is the third position, because counting starts at 0.
  2. Sorted, the numbers would be 1 3 4 6 8 10.
  3. So index 2 is 4. The function did not have to sort everything.

Example 3 — Top three

Split the largest three from the rest, then sort them.

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {5, 1, 9, 3, 7};

    std::nth_element(v.begin(), v.begin() + 2, v.end(), std::greater<int>());
    std::sort(v.begin(), v.begin() + 3, std::greater<int>());

    std::cout << "Top three: ";
    for (auto it = v.begin(); it != v.begin() + 3; ++it) std::cout << *it << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. With std::greater, the "first" positions hold the biggest values.
  2. nth_element() makes sure 9, 7 and 5 sit in the first three slots, in any order.
  3. A small sort() on just those three puts them in order.

Frequently Asked Questions

In C++, std::nth_element() puts the element that would be at position n in a sorted range into place. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The element at the nth position is the one that would be there in a sorted range.
Choose the middle position, call nth_element(), then read the element at that position. For an even count, take the two middle values.
No. Elements before nth are not greater, and elements after are not smaller, but neither side is sorted.

Next: partial_sort()

Sorts only the smallest elements to the front of a range.

partial_sort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

24 people found this page helpful