32 #ifndef ACE_SORTING_SHELL_SORT_H
33 #define ACE_SORTING_SHELL_SORT_H
35 #if ! defined(ACE_SORTING_DIRECT_SHELL_SORT)
42 #define ACE_SORTING_DIRECT_SHELL_SORT 0
45 namespace ace_sorting {
54 #if ACE_SORTING_DIRECT_SHELL_SORT
56 void shellSortClassic(T data[], uint16_t n) {
62 for (uint16_t i = gap; i < n; i++) {
67 for (j = i; j >= gap; j -= gap) {
68 if (data[j - gap] <= temp)
break;
69 data[j] = data[j - gap];
85 auto&& lessThan = [](
const T& a,
const T& b) ->
bool {
return a < b; };
98 template <
typename T,
typename F>
105 for (uint16_t i = gap; i < n; i++) {
110 for (j = i; j >= gap; j -= gap) {
112 if (! lessThan(temp, data[j - gap]))
break;
113 data[j] = data[j - gap];
133 #if ACE_SORTING_DIRECT_SHELL_SORT
134 template <
typename T>
135 void shellSortKnuth(T data[], uint16_t n) {
141 while (gap < n / 3) {
147 for (uint16_t i = gap; i < n; i++) {
152 for (j = i; j >= gap; j -= gap) {
153 if (data[j - gap] <= temp)
break;
154 data[j] = data[j - gap];
168 template <
typename T>
172 auto&& lessThan = [](
const T& a,
const T& b) ->
bool {
return a < b; };
184 template <
typename T,
typename F>
187 while (gap < n / 3) {
193 for (uint16_t i = gap; i < n; i++) {
198 for (j = i; j >= gap; j -= gap) {
200 if (! lessThan(temp, data[j - gap]))
break;
201 data[j] = data[j - gap];
224 #if ACE_SORTING_DIRECT_SHELL_SORT
226 void shellSortTokuda(T data[],
const uint16_t n)
231 static const uint16_t sGaps[] = {
232 1, 4, 9, 20, 46, 103, 233, 525, 1182, 2660, 5985, 13467, 30301,
234 const uint16_t nGaps =
sizeof(sGaps) /
sizeof(uint16_t);
238 for (iGap = 0; sGaps[iGap] < n && iGap < nGaps; iGap++) {}
239 if (iGap != 0) iGap--;
242 uint16_t gap = sGaps[iGap];
245 for (uint16_t i = gap; i < n; i++) {
250 for (j = i; j >= gap; j -= gap) {
251 if (data[j - gap] <= temp)
break;
252 data[j] = data[j - gap];
262 if (iGap == 0)
break;
267 template <
typename T>
271 auto&& lessThan = [](
const T& a,
const T& b) ->
bool {
return a < b; };
284 template<
typename T,
typename F>
290 static const uint16_t sGaps[] = {
291 1, 4, 9, 20, 46, 103, 233, 525, 1182, 2660, 5985, 13467, 30301,
293 const uint16_t nGaps =
sizeof(sGaps) /
sizeof(uint16_t);
297 for (iGap = 0; sGaps[iGap] < n && iGap < nGaps; iGap++) {}
298 if (iGap != 0) iGap--;
301 uint16_t gap = sGaps[iGap];
304 for (uint16_t i = gap; i < n; i++) {
309 for (j = i; j >= gap; j -= gap) {
311 if (! lessThan(temp, data[j - gap]))
break;
312 data[j] = data[j - gap];
322 if (iGap == 0)
break;