|
ADS
Algorithms and Data Structures
|
Shell Sort (section 15.1), a local search that lowers the number of inversions. More...
Files | |
| file | ADS/sort/ShellSort.h |
| Shell Sort (section 15.1), a local search that lowers the number of inversions: an insertion sort of the items \( h \) apart, for decreasing gaps \( h \) down to 1, so that the far items move first and the last pass, the insertion sort, finds the vector almost sorted. | |
Functions | |
| std::vector< std::size_t > | ADS::knuthGaps (std::size_t n) |
| the gaps \( h_1 = 1 \), \( h_{i+1} = 3 h_i + 1 \) below \( n \) (\( 1 , 4 , 13 , 40 , \ldots \)), increasing | |
| std::vector< std::size_t > | ADS::shellGaps (std::size_t n) |
| the gaps \( 1 , \ldots , \lfloor n / 4 \rfloor , \lfloor n / 2 \rfloor \) of Shell, each half the next one, increasing | |
| std::vector< std::size_t > | ADS::hibbardGaps (std::size_t n) |
| the gaps \( 2^k - 1 \) below \( n \) (\( 1 , 3 , 7 , 15 , \ldots \)), increasing | |
| std::vector< std::size_t > | ADS::prattGaps (std::size_t n) |
| the gaps \( 3^i 2^j \) up to \( n \), increasing | |
| std::vector< std::size_t > | ADS::ciuraGaps (std::size_t n) |
| the gaps of Ciura below \( n \), \( 1 , 4 , 10 , \ldots , 701 \) and then each \( 2.25 \) times the one before, increasing | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::shellSort (Store &&A, const std::vector< std::size_t > &gaps, C less=C()) |
| the Shell Sort with the given gaps, increasing and starting from 1 | |
| template<class T, class C = std::less<>> | |
| void | ADS::shellSort (std::vector< T > &A, const std::vector< std::size_t > &gaps, C less=C()) |
| shellSort() 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::shellSort (Store &&A, C less=C()) |
| the Shell Sort of the book, with the gaps \( 1 , 4 , 13 , \ldots \) | |
| template<class T, class C = std::less<>> | |
| void | ADS::shellSort (std::vector< T > &A, C less=C()) |
| shellSort() above on a vector | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { shellSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::shellSort (R &&A, P &&... p) |
| shellSort() 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 | |
Shell Sort (section 15.1), a local search that lowers the number of inversions.
The definitions, the invariants and the costs of the area are in Theory of sorting.
|
inline |
the gaps of Ciura below \( n \), \( 1 , 4 , 10 , \ldots , 701 \) and then each \( 2.25 \) times the one before, increasing
|
inline |
the gaps \( 2^k - 1 \) below \( n \) (\( 1 , 3 , 7 , 15 , \ldots \)), increasing
|
inline |
the gaps \( h_1 = 1 \), \( h_{i+1} = 3 h_i + 1 \) below \( n \) (\( 1 , 4 , 13 , 40 , \ldots \)), increasing
|
inline |
the gaps \( 3^i 2^j \) up to \( n \), increasing
|
inline |
the gaps \( 1 , \ldots , \lfloor n / 4 \rfloor , \lfloor n / 2 \rfloor \) of Shell, each half the next one, increasing
| decltype(auto) ADS::shellSort | ( | R && | A, |
| P &&... | p ) |
shellSort() 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::shellSort | ( | std::vector< T > & | A, |
| C | less = C() ) |
shellSort() above on a vector
| void ADS::shellSort | ( | std::vector< T > & | A, |
| const std::vector< std::size_t > & | gaps, | ||
| C | less = C() ) |
shellSort() above on a vector
| void ADS::shellSort | ( | Store && | A, |
| C | less = C() ) |
the Shell Sort of the book, with the gaps \( 1 , 4 , 13 , \ldots \)
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{shellSort}( A , n ) \cr 2 & \quad \mathrm{shellSort}( A , n , \mathrm{knuthGaps}( n ) ) \end{array}} \]
| void ADS::shellSort | ( | Store && | A, |
| const std::vector< std::size_t > & | gaps, | ||
| C | less = C() ) |
the Shell Sort with the given gaps, increasing and starting from 1
In pseudocode (section 15.1), an insertion sort of the items \( h \) apart for each gap \( h \), the largest first:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{shellSort}( A , n , H ) \cr 2 & \quad \textbf{foreach}\ h \in H \text{ from the largest}\ \textbf{do} \quad \triangleright\ \text{the last } h \text{ is } 1 \cr 3 & \qquad \textbf{for}\ i \gets h\ \textbf{to}\ n - 1\ \textbf{do}\quad \triangleright\ \text{insert } A[ i ] \text{ in its chain} \cr 4 & \qquad\quad \mathit{temp} \gets A[ i ];\ j \gets i \cr 5 & \qquad\quad \textbf{while}\ j \ge h\ \textbf{and}\ \mathit{temp} < A[ j - h ]\ \textbf{do}\ A[ j ] \gets A[ j - h ];\ j \gets j - h \cr 6 & \qquad\quad A[ j ] \gets \mathit{temp} \end{array}} \]