ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Heapsort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Heapsort.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_Heapsort
52 #define __ADS_Heapsort
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#include "ADS/Contract.h"
60#include "ADS/sort/Sorted.h"
61
62#include <algorithm>
63#include <cstddef>
64#include <functional>
65#include <utility>
66#include <vector>
67
68/*--------------------------------------------------------------------------*/
69/*--------------------------- NAMESPACE ------------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72namespace ADS
73{
74
75/*--------------------------------------------------------------------------*/
76
96template< RankStore Store ,
97 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
98 class C = std::less<> >
99void maxHeapRestore( Store && A , std::size_t i , std::size_t dim ,
100 C less = C() );
101
103template< class T , class C = std::less<> >
104void maxHeapRestore( std::vector< T > & A , std::size_t i , std::size_t dim ,
105 C less = C() );
106
119template< RankStore Store ,
120 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
121 class C = std::less<> >
122void heapBuild( Store && A , C less = C() );
123
125template< class T , class C = std::less<> >
126void heapBuild( std::vector< T > & A , C less = C() );
127
143template< RankStore Store ,
144 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
145 class C = std::less<> >
146void heapsort( Store && A , C less = C() );
147
149template< class T , class C = std::less<> >
150void heapsort( std::vector< T > & A , C less = C() );
151
155template< RankStore Store ,
156 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
157 class C = std::less<> >
158bool isMaxHeap( Store && A , std::size_t dim , C less = C() );
159
161template< class T , class C = std::less<> >
162bool isMaxHeap( const std::vector< T > & A , std::size_t dim , C less = C() );
163
164/*--------------------------------------------------------------------------*/
165/*------------------------- THE SORTS OF A RANGE ---------------------------*/
166/*--------------------------------------------------------------------------*/
167
172template< SortRange R , class... P >
173 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
174 P &&... p )
175{ heapsort( v , std::forward< P >( p )... ); }
176decltype( auto ) heapsort( R && A , P &&... p )
177{
178 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
179 return( heapsort( v , std::forward< P >( p )... ) );
180 };
181 return( detail::sortRange( A , f , f ) );
182 }
183
184/*--------------------------------------------------------------------------*/
185
186} // end( namespace ADS )
187
188/*--------------------------------------------------------------------------*/
189/*-------------------------- IMPLEMENTATION --------------------------------*/
190/*--------------------------------------------------------------------------*/
191
192#include "ADS/sort/impl/Heapsort.tpp"
193
194/*--------------------------------------------------------------------------*/
195
196#endif /* Heapsort.h included */
197
198/*--------------------------------------------------------------------------*/
199/*-------------------------- End File Heapsort.h ---------------------------*/
200/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
the storage on which the sorts of the book work in place, which read the items by their ranks only: a...
Definition Sorted.h:184
a range that the sorts take beside std::vector and std::span: random access, sized,...
Definition Sorted.h:153
void heapBuild(Store &&A, C less=C())
made a max-heap, time
void maxHeapRestore(Store &&A, std::size_t i, std::size_t dim, C less=C())
moved down in the heap , with the order less, extra space
void heapsort(Store &&A, C less=C())
in increasing order, time
bool isMaxHeap(Store &&A, std::size_t dim, C less=C())
true if is a max-heap, i.e., if no item is larger than its parent
Definition ADT.h:96