C++ std::partial_sort() Function

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

Definition

In C++, std::partial_sort() rearranges a range so the first few positions hold the smallest elements in sorted order. The rest of the range is left in an unspecified order.

It is faster than a full sort when you only need the top few results, such as the best three scores.

Syntax

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

C++
std::partial_sort(v.begin(), v.begin() + 3, v.end());
std::partial_sort(v.begin(), v.begin() + 3, v.end(), std::greater<int>());
CallWhat it does
std::partial_sort(v.begin(), v.begin() + 3, v.end());Put the 3 smallest elements first, in order.
std::partial_sort(v.begin(), v.begin() + 3, v.end(), std::greater<int>());Put the 3 largest elements first.
Library declarations
C++
template<class RandomIt>
void partial_sort(RandomIt first, RandomIt middle, RandomIt last);

template<class RandomIt, class Compare>
void partial_sort(RandomIt first, RandomIt middle, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The whole range.
middleRequired. The end of the sorted front part. Elements from first to middle are sorted.
compOptional. A comparison function.

Return Value

Return ValueDescription
voidNothing is returned. The range is changed in place.

C++ Examples

Example 1 — Smallest three

Find the three smallest numbers in order.

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

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

    std::partial_sort(v.begin(), v.begin() + 3, v.end());

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

How It Works

  1. v.begin() + 3 is the middle, so three elements are sorted.
  2. The smallest values 1, 3 and 4 are moved to the front in order.
  3. We print only the sorted part, because the rest is unspecified.

Example 2 — Top three scores

Use std::greater to get the biggest values first.

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

int main() {
    std::vector<int> scores = {55, 90, 72, 38, 81, 66};

    std::partial_sort(scores.begin(), scores.begin() + 3, scores.end(), std::greater<int>());

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

How It Works

  1. std::greater<int>() changes "smallest first" into "largest first".
  2. The three best scores are 90, 81 and 72.
  3. The remaining three scores are in no promised order.

Example 3 — First names alphabetically

Get the first two names in alphabetical order.

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

int main() {
    std::vector<std::string> names = {"Zoe", "Ann", "Mia", "Bob"};

    std::partial_sort(names.begin(), names.begin() + 2, names.end());

    std::cout << "First two: ";
    for (auto it = names.begin(); it != names.begin() + 2; ++it) std::cout << *it << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. Strings are compared alphabetically by default.
  2. Ann and Bob are the first two in alphabetical order.
  3. Mia and Zoe are left after the middle in unspecified order.

Frequently Asked Questions

In C++, std::partial_sort() sorts only the smallest elements to the front of a range. 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 first part of the range is sorted and holds the smallest elements.
When you only need the first k sorted elements. It can do less work than sorting everything.
No. They are still in the vector, but their order is unspecified.

Next: partial_sort_copy()

Copies the smallest elements of a range into a sorted destination.

partial_sort_copy() 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.

23 people found this page helpful