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

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

Detailed description

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:

  • knuthGaps(), the \( h = ( 3^i - 1 ) / 2 \), i.e., \( 1 , 4 , 13 , 40 , 121 , \ldots \) of the book, \( O( n^{1.5} ) \) in the worst case;
  • shellGaps(), the \( n / 2 , n / 4 , \ldots , 1 \) of Shell, \( \Theta( n^2 ) \) in the worst case;
  • hibbardGaps(), the \( 2^k - 1 = 1 , 3 , 7 , 15 , \ldots \) of Hibbard, which unlike those of Shell are coprime, \( O( n^{1.5} ) \) in the worst case;
  • prattGaps(), all the \( 3^i 2^j \) up to \( n \), \( O( n \log^2 n ) \) in the worst case;
  • ciuraGaps(), \( 1 , 4 , 10 , 23 , 57 , 132 , 301 , 701 \) and then \( \times 2.25 \), found by experiment and among the fastest in practice.

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.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 15.1, https://algoritmicamente.it/materiali/libro/capitoli/cap15.pdf
D. L. Shell, A high-speed sorting procedure, Communications of the ACM 2(7), 30-32, 1959, doi:10.1145/368370.368387
T. N. Hibbard, An empirical study of minimal storage sorting, Communications of the ACM 6(5), 206-213, 1963, doi:10.1145/366552.366557
V. R. Pratt, Shellsort and sorting networks, PhD thesis, Stanford University, 1971
M. Ciura, Best increments for the average case of Shellsort, FCT 2001, LNCS 2138, 106-117, doi:10.1007/3-540-44669-9_12
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa