Home C++ Algorithms Functions partial_sort() C++ std::partial_sort() Function 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.
std::partial_sort(v.begin(), v.begin() + 3, v.end());
std::partial_sort(v.begin(), v.begin() + 3, v.end(), std::greater<int>()); Call What 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 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 Parameter Description first, last Required. The whole range. middle Required. The end of the sorted front part. Elements from first to middle are sorted. comp Optional. A comparison function.
Return Value Return Value Description voidNothing is returned. The range is changed in place.
C++ Examples 1. Smallest three 2. Top three scores 3. First names alphabetically Example 1 — Smallest three Find the three smallest numbers in order.
#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 v.begin() + 3 is the middle, so three elements are sorted.The smallest values 1, 3 and 4 are moved to the front in order. 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.
#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 std::greater<int>() changes "smallest first" into "largest first".The three best scores are 90, 81 and 72. The remaining three scores are in no promised order. Example 3 — First names alphabetically Get the first two names in alphabetical order.
#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 Strings are compared alphabetically by default. Ann and Bob are the first two in alphabetical order.Mia and Zoe are left after the middle in unspecified order.Frequently Asked Questions What does std::partial_sort() do? In C++, std::partial_sort() sorts only the smallest elements to the front of a range. Include <algorithm> to use it.
Which header and C++ version do I need for std::partial_sort()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::partial_sort() return? It returns nothing (void). The first part of the range is sorted and holds the smallest elements.
When is partial_sort() better than sort()? When you only need the first k sorted elements. It can do less work than sorting everything.
Are the other elements lost? No. They are still in the vector, but their order is unspecified.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion