|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include "ADS/sort/Sorted.h"#include <algorithm>#include <cstddef>#include <functional>#include <vector>#include "ADS/sort/impl/ShellSort.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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: 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.
In place, iterative, not stable; its time depends on the gaps:
The contracts [see Contract.h]: the five generators give gaps that start from 1 and increase; shellSort() needs positive increasing gaps, and the vector ends with the same items, sorted when the first gap is 1 (with a single gap \( h \) it is only \( h \)-sorted, as in figure 15.4), an audit. The functions are defined in ShellSort.tpp.