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)
insertionSort.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_INSERTION_SORT_H
33 #define ACE_SORTING_INSERTION_SORT_H
34 
35 #if ! defined(ACE_SORTING_DIRECT_INSERTION_SORT)
36 
42  #define ACE_SORTING_DIRECT_INSERTION_SORT 0
43 #endif
44 
45 namespace ace_sorting {
46 
54 #if ACE_SORTING_DIRECT_INSERTION_SORT
55 template <typename T>
56 void insertionSort(T data[], uint16_t n) {
57  for (uint16_t i = 1; i < n; i++) {
58  T temp = data[i];
59 
60  // Shift one slot to the right.
61  uint16_t j;
62  for (j = i; j > 0; j--) {
63  if (data[j - 1] <= temp) break;
64  data[j] = data[j - 1];
65  }
66 
67  // This can assign 'temp' back into the original slot if no shifting was
68  // done. That's ok because T is assumed to be relatively cheap to copy, and
69  // checking for (i != j) is more expensive than just doing the extra
70  // assignment.
71  data[j] = temp;
72  }
73 }
74 #else
75 template <typename T>
76 void insertionSort(T data[], uint16_t n) {
77  // This lambda expression does not perform any captures, so the compiler will
78  // optimize and inline the less-than expression.
79  auto&& lessThan = [](const T& a, const T& b) -> bool { return a < b; };
80  insertionSort(data, n, lessThan);
81 }
82 #endif
83 
91 template <typename T, typename F>
92 void insertionSort(T data[], uint16_t n, F&& lessThan) {
93  for (uint16_t i = 1; i < n; i++) {
94  T temp = data[i];
95 
96  // Shift one slot to the right.
97  uint16_t j;
98  for (j = i; j > 0; j--) {
99  // The following is equivalent to: (data[j - 1] <= temp)
100  if (! lessThan(temp, data[j - 1])) break;
101  data[j] = data[j - 1];
102  }
103 
104  // This can assign 'temp' back into the original slot if no shifting was
105  // done. That's ok because T is assumed to be relatively cheap to copy, and
106  // checking for (i != j) is more expensive than just doing the extra
107  // assignment.
108  data[j] = temp;
109  }
110 }
111 
112 }
113 
114 #endif
ace_sorting::insertionSort
void insertionSort(T data[], uint16_t n)
Insertion sort.
Definition: insertionSort.h:76