C++ std::sort_heap() Function

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

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.

C++
std::sort_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end(), std::greater<int>());
CallWhat 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
C++
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

ParameterDescription
first, lastRequired. A range that is already a heap.
compOptional. The comparison used to build the heap.

Return Value

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

C++ Examples

Example 1 — Heap sort

Build a heap, then sort it.

C++
#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

  1. make_heap() creates the heap that sort_heap() needs.
  2. sort_heap() repeatedly moves the largest value to the end.
  3. The result is in ascending order.

Example 2 — Descending order

Use std::greater for both calls.

C++
#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

  1. Both calls use std::greater<int>(), so the heap is a min-heap.
  2. Sorting by "greater" puts the larger numbers first.
  3. The vector ends up in descending order.

Example 3 — Check the result

Confirm that the range is sorted.

C++
#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

  1. std::is_sorted() asks if every value is not less than the one before.
  2. After sort_heap() the answer is true.
  3. 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.

Next: stable_partition()

Reorders a range into two groups and keeps the order inside each group.

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

21 people found this page helpful