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)
selectionSort.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_SELECTION_SORT_H
33 #define ACE_SORTING_SELECTION_SORT_H
34 
35 #include "swap.h"
36 
37 namespace ace_sorting {
38 
39 #if ! defined(ACE_SORTING_DIRECT_SELECTION_SORT)
40 
46  #define ACE_SORTING_DIRECT_SELECTION_SORT 0
47 #endif
48 
56 #if ACE_SORTING_DIRECT_SELECTION_SORT
57 template <typename T>
58 void selectionSort(T data[], uint16_t n) {
59  for (uint16_t i = 0; i < n; i++) {
60 
61  // Loop to find the smallest element.
62  uint16_t iSmallest = i;
63  T smallest = data[i];
64 
65  // Starting the loop with 'j = i + 1' increases flash usage on AVR by 12
66  // bytes. But it does not reduce the execution time signficantly, because
67  // the (i + 1) will be done anyway by the j++ in the loop. So the only thing
68  // we save is a single redundant 'smallest < smallest' comparison.
69  for (uint16_t j = i; j < n; j++) {
70  if (data[j] < smallest) {
71  iSmallest = j;
72  smallest = data[j];
73  }
74  }
75 
76  // This extra check (i != iSmallest) is not really necessary, because if the
77  // first element was already the smallest, it would swap the value back into
78  // itself. However, the one situation where Selection Sort *might* be used
79  // over Insertion Sort is when the write operation is far more expensive
80  // than a read operation. So this test preserves that advantage of the
81  // Selection Sort, by avoiding doing an unnecessary swap.
82  if (i != iSmallest) {
83  swap(data[i], data[iSmallest]);
84  }
85  }
86 }
87 #else
88 template <typename T>
89 void selectionSort(T data[], uint16_t n) {
90  // This lambda expression does not perform any captures, so the compiler will
91  // optimize and inline the less-than expression.
92  auto&& lessThan = [](const T& a, const T& b) -> bool { return a < b; };
93  selectionSort(data, n, lessThan);
94 }
95 #endif
96 
104 template <typename T, typename F>
105 void selectionSort(T data[], uint16_t n, F&& lessThan) {
106  for (uint16_t i = 0; i < n; i++) {
107 
108  // Loop to find the smallest element.
109  uint16_t iSmallest = i;
110  T smallest = data[i];
111 
112  // Starting the loop with 'j = i + 1' increases flash usage on AVR by 12
113  // bytes. But it does not reduce the execution time signficantly, because
114  // the (i + 1) will be done anyway by the j++ in the loop. So the only thing
115  // we save is a single redundant 'smallest < smallest' comparison.
116  for (uint16_t j = i; j < n; j++) {
117  if (lessThan(data[j], smallest)) {
118  iSmallest = j;
119  smallest = data[j];
120  }
121  }
122 
123  // This extra check (i != iSmallest) is not really necessary, because if the
124  // first element was already the smallest, it would swap the value back into
125  // itself. However, the one situation where Selection Sort *might* be used
126  // over Insertion Sort is when the write operation is far more expensive
127  // than a read operation. So this test preserves that advantage of the
128  // Selection Sort, by avoiding doing an unnecessary swap.
129  if (i != iSmallest) {
130  swap(data[i], data[iSmallest]);
131  }
132  }
133 }
134 
135 }
136 
137 #endif
swap.h
ace_sorting::swap
void swap(T &a, T &b)
Swap the parameters.
Definition: swap.h:41
ace_sorting::selectionSort
void selectionSort(T data[], uint16_t n)
Selection sort.
Definition: selectionSort.h:89