AceSorting  1.0.0
Sorting algorithms for Arduino including Bubble Sort, Insertion Sort, Selection Sort, Shell Sort (3 versions), Comb Sort (4 versions), Quick Sort (3 versions)
shellSort.h
Go to the documentation of this file.
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
32 #ifndef ACE_SORTING_SHELL_SORT_H
33 #define ACE_SORTING_SHELL_SORT_H
34 
35 #if ! defined(ACE_SORTING_DIRECT_SHELL_SORT)
36 
42  #define ACE_SORTING_DIRECT_SHELL_SORT 0
43 #endif
44 
45 namespace ace_sorting {
46 
54 #if ACE_SORTING_DIRECT_SHELL_SORT
55 template <typename T>
56 void shellSortClassic(T data[], uint16_t n) {
57  uint16_t gap = n;
58  while (gap > 1) {
59  gap /= 2;
60 
61  // Do insertion sort of each sub-array separated by gap.
62  for (uint16_t i = gap; i < n; i++) {
63  T temp = data[i];
64 
65  // Shift one slot to the right.
66  uint16_t j;
67  for (j = i; j >= gap; j -= gap) {
68  if (data[j - gap] <= temp) break;
69  data[j] = data[j - gap];
70  }
71 
72  // Just like insertionSort(), this can assign 'temp' back into the
73  // original slot if no shifting was done. That's ok because T is assumed
74  // to be relatively cheap to copy, and checking for (i != j) is more
75  // expensive than just doing the extra assignment.
76  data[j] = temp;
77  }
78  }
79 }
80 #else
81 template <typename T>
82 void shellSortClassic(T data[], uint16_t n) {
83  // This lambda expression does not perform any captures, so the compiler will
84  // optimize and inline the less-than expression.
85  auto&& lessThan = [](const T& a, const T& b) -> bool { return a < b; };
86  shellSortClassic(data, n, lessThan);
87 }
88 #endif
89 
98 template <typename T, typename F>
99 void shellSortClassic(T data[], uint16_t n, F&& lessThan) {
100  uint16_t gap = n;
101  while (gap > 1) {
102  gap /= 2;
103 
104  // Do insertion sort of each sub-array separated by gap.
105  for (uint16_t i = gap; i < n; i++) {
106  T temp = data[i];
107 
108  // Shift one slot to the right.
109  uint16_t j;
110  for (j = i; j >= gap; j -= gap) {
111  // The following is equivalent to: (data[j - gap] <= temp)
112  if (! lessThan(temp, data[j - gap])) break;
113  data[j] = data[j - gap];
114  }
115 
116  // Just like insertionSort(), this can assign 'temp' back into the
117  // original slot if no shifting was done. That's ok because T is assumed
118  // to be relatively cheap to copy, and checking for (i != j) is more
119  // expensive than just doing the extra assignment.
120  data[j] = temp;
121  }
122  }
123 }
124 
125 //-----------------------------------------------------------------------------
126 
133 #if ACE_SORTING_DIRECT_SHELL_SORT
134 template <typename T>
135 void shellSortKnuth(T data[], uint16_t n) {
136  // Calculate the largest gap using Knuth's formula. If n is a compile-time
137  // constant and relatively "small" (observed to be true at least up to 100),
138  // the compiler will precalculate the loop below and replace it with a
139  // compile-time constant.
140  uint16_t gap = 1;
141  while (gap < n / 3) {
142  gap = gap * 3 + 1;
143  }
144 
145  while (gap > 0) {
146  // Do insertion sort of each sub-array separated by gap.
147  for (uint16_t i = gap; i < n; i++) {
148  T temp = data[i];
149 
150  // Shift one slot to the right.
151  uint16_t j;
152  for (j = i; j >= gap; j -= gap) {
153  if (data[j - gap] <= temp) break;
154  data[j] = data[j - gap];
155  }
156 
157  // Just like insertionSort(), this can assign 'temp' back into the
158  // original slot if no shifting was done. That's ok because T is assumed
159  // to be relatively cheap to copy, and checking for (i != j) is more
160  // expensive than just doing the extra assignment.
161  data[j] = temp;
162  }
163 
164  gap = (gap - 1) / 3;
165  }
166 }
167 #else
168 template <typename T>
169 void shellSortKnuth(T data[], uint16_t n) {
170  // This lambda expression does not perform any captures, so the compiler will
171  // optimize and inline the less-than expression.
172  auto&& lessThan = [](const T& a, const T& b) -> bool { return a < b; };
173  shellSortKnuth(data, n, lessThan);
174 }
175 #endif
176 
184 template <typename T, typename F>
185 void shellSortKnuth(T data[], uint16_t n, F&& lessThan) {
186  uint16_t gap = 1;
187  while (gap < n / 3) {
188  gap = gap * 3 + 1;
189  }
190 
191  while (gap > 0) {
192  // Do insertion sort of each sub-array separated by gap.
193  for (uint16_t i = gap; i < n; i++) {
194  T temp = data[i];
195 
196  // Shift one slot to the right.
197  uint16_t j;
198  for (j = i; j >= gap; j -= gap) {
199  // The following is equivalent to: (data[j - gap] <= temp)
200  if (! lessThan(temp, data[j - gap])) break;
201  data[j] = data[j - gap];
202  }
203 
204  // Just like insertionSort(), this can assign 'temp' back into the
205  // original slot if no shifting was done. That's ok because T is assumed
206  // to be relatively cheap to copy, and checking for (i != j) is more
207  // expensive than just doing the extra assignment.
208  data[j] = temp;
209  }
210 
211  gap = (gap - 1) / 3;
212  }
213 }
214 
215 //-----------------------------------------------------------------------------
216 
224 #if ACE_SORTING_DIRECT_SHELL_SORT
225 template<typename T>
226 void shellSortTokuda(T data[], const uint16_t n)
227 {
228  // Experimentally observed ideal gaps.
229  // https://en.wikipedia.org/wiki/Shellsort
230  // https://oeis.org/A108870
231  static const uint16_t sGaps[] = {
232  1, 4, 9, 20, 46, 103, 233, 525, 1182, 2660, 5985, 13467, 30301,
233  };
234  const uint16_t nGaps = sizeof(sGaps) / sizeof(uint16_t);
235 
236  // Find the starting gap.
237  uint16_t iGap;
238  for (iGap = 0; sGaps[iGap] < n && iGap < nGaps; iGap++) {}
239  if (iGap != 0) iGap--;
240 
241  while (true) {
242  uint16_t gap = sGaps[iGap];
243 
244  // Do insertion sort of each sub-array separated by gap.
245  for (uint16_t i = gap; i < n; i++) {
246  T temp = data[i];
247 
248  // Shift one slot to the right.
249  uint16_t j;
250  for (j = i; j >= gap; j -= gap) {
251  if (data[j - gap] <= temp) break;
252  data[j] = data[j - gap];
253  }
254 
255  // Just like insertionSort(), this can assign 'temp' back into the
256  // original slot if no shifting was done. That's ok because T is assumed
257  // to be relatively cheap to copy, and checking for (i != j) is more
258  // expensive than just doing the extra assignment.
259  data[j] = temp;
260  }
261 
262  if (iGap == 0) break;
263  iGap--;
264  }
265 }
266 #else
267 template <typename T>
268 void shellSortTokuda(T data[], uint16_t n) {
269  // This lambda expression does not perform any captures, so the compiler will
270  // optimize and inline the less-than expression.
271  auto&& lessThan = [](const T& a, const T& b) -> bool { return a < b; };
272  shellSortTokuda(data, n, lessThan);
273 }
274 #endif
275 
284 template<typename T, typename F>
285 void shellSortTokuda(T data[], const uint16_t n, F&& lessThan)
286 {
287  // Experimentally observed ideal gaps.
288  // https://en.wikipedia.org/wiki/Shellsort
289  // https://oeis.org/A108870
290  static const uint16_t sGaps[] = {
291  1, 4, 9, 20, 46, 103, 233, 525, 1182, 2660, 5985, 13467, 30301,
292  };
293  const uint16_t nGaps = sizeof(sGaps) / sizeof(uint16_t);
294 
295  // Find the starting gap.
296  uint16_t iGap;
297  for (iGap = 0; sGaps[iGap] < n && iGap < nGaps; iGap++) {}
298  if (iGap != 0) iGap--;
299 
300  while (true) {
301  uint16_t gap = sGaps[iGap];
302 
303  // Do insertion sort of each sub-array separated by gap.
304  for (uint16_t i = gap; i < n; i++) {
305  T temp = data[i];
306 
307  // Shift one slot to the right.
308  uint16_t j;
309  for (j = i; j >= gap; j -= gap) {
310  // The following is equivalent to: (data[j - gap] <= temp)
311  if (! lessThan(temp, data[j - gap])) break;
312  data[j] = data[j - gap];
313  }
314 
315  // Just like insertionSort(), this can assign 'temp' back into the
316  // original slot if no shifting was done. That's ok because T is assumed
317  // to be relatively cheap to copy, and checking for (i != j) is more
318  // expensive than just doing the extra assignment.
319  data[j] = temp;
320  }
321 
322  if (iGap == 0) break;
323  iGap--;
324  }
325 }
326 
327 }
328 
329 #endif
ace_sorting::shellSortTokuda
void shellSortTokuda(T data[], uint16_t n)
Shell sort using gap sizes empirically determined by Tokuda.
Definition: shellSort.h:268
ace_sorting::shellSortKnuth
void shellSortKnuth(T data[], uint16_t n)
Shell sort using gap size from Knuth.
Definition: shellSort.h:169
ace_sorting::shellSortClassic
void shellSortClassic(T data[], uint16_t n)
Shell sort with gap size reduced by factor of 2 each iteration.
Definition: shellSort.h:82