C++ std::sort_heap() Function

Definition
In C++, std::sort_heap() takes a range that is already a heap and sorts it. After the call the range is no longer a heap.
It is the last step of a heap sort: make_heap() first, then sort_heap().
Syntax
Add #include <algorithm>, then call std::sort_heap(). Start with these two lines.
std::sort_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end(), std::greater<int>());
| Call | What it does |
|---|
std::sort_heap(v.begin(), v.end()); | Sort a max-heap into ascending order. |
std::sort_heap(v.begin(), v.end(), std::greater<int>()); | Sort a min-heap into descending order. |
Library declarations
template<class RandomIt>
void sort_heap(RandomIt first, RandomIt last);
template<class RandomIt, class Compare>
void sort_heap(RandomIt first, RandomIt last, Compare comp);
Parameters
| Parameter | Description |
|---|
| first, last | Required. A range that is already a heap. |
| comp | Optional. The comparison used to build the heap. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. The range is sorted in place. |
C++ Examples
Example 1 — Heap sort
Build a heap, then sort it.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {4, 1, 7, 3, 9};
std::make_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
make_heap() creates the heap that sort_heap() needs.sort_heap() repeatedly moves the largest value to the end.- The result is in ascending order.
Example 2 — Descending order
Use std::greater for both calls.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {4, 1, 7, 3, 9};
std::make_heap(v.begin(), v.end(), std::greater<int>());
std::sort_heap(v.begin(), v.end(), std::greater<int>());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- Both calls use
std::greater<int>(), so the heap is a min-heap. - Sorting by "greater" puts the larger numbers first.
- The vector ends up in descending order.
Example 3 — Check the result
Confirm that the range is sorted.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 2, 8, 1};
std::make_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end());
std::cout << std::boolalpha << "Sorted: " << std::is_sorted(v.begin(), v.end()) << '\n';
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
std::is_sorted() asks if every value is not less than the one before.- After
sort_heap() the answer is true. - The printed vector shows 1, 2, 5 and 8.
Frequently Asked Questions
In C++, std::sort_heap() turns a heap into a sorted 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 heap is turned into a sorted range.
Not usually. std::sort() is normally faster. sort_heap() is useful when you already have a heap.
No. A sorted range is not a heap any more.
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.
21 people found this page helpful