ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Heapsort.h File Reference

The max-heap on a vector and heapsort (section 10.1.4): the children of the place \( i \) are \( 2 i + 1 \) and \( 2 i + 2 \), and a max-heap has \( A[ \lfloor ( i - 1 ) / 2 \rfloor ] \ge A[ i ] \) for each \( i > 0 \). More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/Heapsort.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::maxHeapRestore (Store &&A, std::size_t i, std::size_t dim, C less=C())
 \( A[ i ] \) moved down in the heap \( A[ 0 .. \mathit{dim} - 1 ] \), with the order less, \( O( 1 ) \) extra space
template<class T, class C = std::less<>>
void ADS::maxHeapRestore (std::vector< T > &A, std::size_t i, std::size_t dim, C less=C())
 maxHeapRestore() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::heapBuild (Store &&A, C less=C())
 \( A \) made a max-heap, \( O( n ) \) time
template<class T, class C = std::less<>>
void ADS::heapBuild (std::vector< T > &A, C less=C())
 heapBuild() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::heapsort (Store &&A, C less=C())
 \( A \) in increasing order, \( O( n \log n ) \) time
template<class T, class C = std::less<>>
void ADS::heapsort (std::vector< T > &A, C less=C())
 heapsort() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
bool ADS::isMaxHeap (Store &&A, std::size_t dim, C less=C())
 true if \( A[ 0 .. \mathit{dim} - 1 ] \) is a max-heap, i.e., if no item is larger than its parent
template<class T, class C = std::less<>>
bool ADS::isMaxHeap (const std::vector< T > &A, std::size_t dim, C less=C())
 isMaxHeap() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { heapsort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::heapsort (R &&A, P &&... p)
 heapsort() on the range \( A \) [see SortRange], the other arguments as for a vector, in place: on the span of its items if \( A \) is contiguous, on its view otherwise, since the sort reads the items by their ranks only

Detailed description

The max-heap on a vector and heapsort (section 10.1.4): the children of the place \( i \) are \( 2 i + 1 \) and \( 2 i + 2 \), and a max-heap has \( A[ \lfloor ( i - 1 ) / 2 \rfloor ] \ge A[ i ] \) for each \( i > 0 \).

maxHeapRestore( A , i , dim ) moves the item in \( A[ i ] \) down, swapping it with its largest child, when the subtrees of the children are max-heaps in \( A[ 0 .. \mathit{dim} - 1 ] \): \( O( m ) \) time on a node of height \( m \). heapBuild() calls it from \( \lfloor n / 2 \rfloor - 1 \) down to 0, the places from \( \lfloor n / 2 \rfloor \) on being leaves; since a heap of \( n \) items has at most \( \lceil n / 2^{m+1} \rceil \) nodes of height \( m \), its time is

\[ O\left( \sum_{m=0}^{\lfloor \log n \rfloor} \frac{n}{2^{m+1}} m \right) = O\left( n \sum_{m=0}^{\infty} \frac{m}{2^m} \right) = O( n ), \]

the series being 2. heapsort() builds the heap and then swaps the maximum \( A[ 0 ] \) with the last place of the heap, which shrinks by one, and restores it, in \( O( n \log n ) \) time and \( O( 1 ) \) extra space, since maxHeapRestore() is a loop rather than the recursion of the book (exercise 10.4). Not stable.

The contracts [see Contract.h]: maxHeapRestore() and isMaxHeap() need \( \mathit{dim} \le n \), maxHeapRestore() also \( i < \mathit{dim} \), and heapBuild() ends with a heap and heapsort() with the same items sorted, two audits. The functions are defined in Heapsort.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 10.1.4, https://algoritmicamente.it/materiali/libro/capitoli/cap10.pdf
J. W. J. Williams, Algorithm 232: Heapsort, Communications of the ACM 7(6), 347-348, 1964, doi:10.1145/512274.3734138
R. W. Floyd, Algorithm 245: Treesort 3, Communications of the ACM 7(12), 701, 1964, doi:10.1145/355588.365103
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa