|
ADS
Algorithms and Data Structures
|
The max-heap on a vector and heapsort (section 10.1.4). More...
Files | |
| file | ADS/sort/Heapsort.h |
| 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 \). | |
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 | |
The max-heap on a vector and heapsort (section 10.1.4).
The definitions, the invariants and the costs of the area are in Theory of sorting.
| void ADS::heapBuild | ( | std::vector< T > & | A, |
| C | less = C() ) |
heapBuild() above on a vector
| void ADS::heapBuild | ( | Store && | A, |
| C | less = C() ) |
\( A \) made a max-heap, \( O( n ) \) time
In pseudocode (section 10.1.4), the leaves being heaps already:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{heapBuild}( A , n ) \cr 2 & \quad \textbf{for}\ i \gets \lfloor n / 2 \rfloor - 1\ \textbf{downto} \ 0\ \textbf{do}\quad \triangleright\ \text{the leaves are heaps} \cr 3 & \qquad \mathrm{maxHeapRestore}( A , i , n ) \end{array}} \]
| 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
| void ADS::heapsort | ( | std::vector< T > & | A, |
| C | less = C() ) |
heapsort() above on a vector
| void ADS::heapsort | ( | Store && | A, |
| C | less = C() ) |
\( A \) in increasing order, \( O( n \log n ) \) time
In pseudocode (section 10.1.4), \( A[ i .. n - 1 ] \) holding the largest items in order and \( A[ 0 .. i - 1 ] \) a heap of the others:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{heapsort}( A , n ) \cr 2 & \quad \mathrm{heapBuild}( A , n ) \cr 3 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 1\ \textbf{do} \quad \triangleright\ A[ i + 1 .. n - 1 ] \text{ sorted} \cr 4 & \qquad A[ 0 ] \leftrightarrow A[ i ];\ \mathrm{maxHeapRestore}( A , 0 , i ) \end{array}} \]
| bool ADS::isMaxHeap | ( | const std::vector< T > & | A, |
| std::size_t | dim, | ||
| C | less = C() ) |
isMaxHeap() above on a vector
| 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
| void ADS::maxHeapRestore | ( | std::vector< T > & | A, |
| std::size_t | i, | ||
| std::size_t | dim, | ||
| C | less = C() ) |
maxHeapRestore() above on a vector
| 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
In pseudocode (section 10.1.4, a loop instead of the recursion of the book), the subtrees of the children of \( i \) being max-heaps:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{maxHeapRestore}( A , i , \mathit{dim} ) \cr 2 & \quad \textbf{loop}\quad \triangleright\ \text{only } A[ i ] \text{ may be smaller than a child} \cr 3 & \qquad \mathit{max} \gets \text{the index of the largest of } A[ i ] , A[ 2 i + 1 ] , A[ 2 i + 2 ] \cr & \qquad\quad \text{among those below } \mathit{dim} \cr 4 & \qquad \textbf{if}\ \mathit{max} = i\ \textbf{then return} \cr 5 & \qquad A[ i ] \leftrightarrow A[ \mathit{max} ];\ i \gets \mathit{max} \quad \triangleright\ \text{one level down} \end{array}} \]