ranges::sort_heap

ranges::sort_heap (힙을 정렬로 — ranges)

힙을 정렬된 범위로 변환하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::sort_heap은 힙 [first, last)을 정렬된 비감소 범위로 변환해요.

namespace std::ranges {
template< class R, class Comp = ranges::less, class Proj = std::identity >
requires std::random_access_range<R> && std::sortable<ranges::iterator_t<R>, Comp, Proj>
constexpr ranges::borrowed_iterator_t<R>
    sort_heap( R&& r, Comp comp = {}, Proj proj = {} );
}
  • 복잡도: 최악의 경우 N·log N번의 비교.
  • 힙이 아니면 동작이 미정의예요.
std::vector<int> v{9, 5, 6, 1, 3};
std::ranges::make_heap(v);
std::ranges::sort_heap(v);
// v == {1,3,5,6,9} 정렬됨

힙을 정렬 순서로 바꾸는 ranges 버전이에요. 힙 정렬(heapsort)의 마지막 단계에 해당해요.

더 알아보기 (Learn more)

cppreference