-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.hpp
More file actions
129 lines (111 loc) · 3.98 KB
/
SelectionSort.hpp
File metadata and controls
129 lines (111 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef ALGORITHMS_SELECTIONSORT_HPP
#define ALGORITHMS_SELECTIONSORT_HPP
#include <span> // std::span, std::array, std::vector
#include "Comparable.hpp" // includes Comparable concept used as a constraint
using namespace std;
/**
* The {@code SelectionSort} class uses selection sort to
* sort a container through invoking its
* constructor with the container variable.
*
* This implementation makes ~ ½ n^2 compares to sort
* any array of length n, so it is not suitable for sorting large arrays.
* It performs exactly n exchanges.
*
* @author Benjamin Chan
*
* Adapted from Algorithms, 4th edition, {@authors Robert Sedgewick and Kevin Wayne}
* and their booksite https://algs4.cs.princeton.edu/
*
* The Java program from which this C++ code was adapted from is found at
* https://algs4.cs.princeton.edu/21elementary/Selection.java.html.
*
* @param <T> the generic type of an item in this sorting algorithm
*/
template<typename T> requires Comparable<T>
class SelectionSort {
public:
/**
* Rearranges the container in ascending order, using the natural order, or descending order.
*
* @param a, the container to be sorted
* @param a boolean specifying whether it should be reverse
*/
explicit SelectionSort<T>(span<T> a, bool reverse = false) {
if (!reverse) {
int n = a.size();
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[min]) min = j;
}
exch(a, i, min);
assert(isSorted(a, 0, i, reverse));
}
} else {
int n = a.size();
for (int i = 0; i < n; i++) {
int max = i;
for (int j = i + 1; j < n; j++) {
if (a[j] > a[max]) max = j;
}
exch(a, i, max);
assert(isSorted(a, 0, i, reverse));
}
}
assert(isSorted(a, reverse));
};
private:
// exchange a[i] and a[j]
void exch(span<T> a, int i, int j);
// check if entire container is sorted -- useful for debugging
bool isSorted(span<T> a, int lo, int hi, bool reverse = false);
// check if container is sorted between two indices, lo and hi -- useful for debugging
bool isSorted(span<T> a, bool reverse = false);
};
template<typename T>
requires Comparable<T>
void SelectionSort<T>::exch(span<T> a, int i, int j) {
T swap = a[i];
a[i] = a[j];
a[j] = swap;
}
template<typename T>
requires Comparable<T>
bool SelectionSort<T>::isSorted(span<T> a, bool reverse) {
return isSorted(a, 0, a.size() - 1, reverse);
}
template<typename T>
requires Comparable<T>
bool SelectionSort<T>::isSorted(span<T> a, int lo, int hi, bool reverse) {
if (!reverse) {
for (int i = 1; i <= hi; i++)
if (a[i] < a[i - 1]) return false;
return true;
} else {
for (int i = 1; i <= hi; i++)
if (a[i] > a[i - 1]) return false;
return true;
}
}
/**
* Deduct the type, <T>, of the InsertionSort class based on constructor argument types
* and number of arguments
*/
template<typename T> requires Comparable<T>
SelectionSort(span<T>) -> SelectionSort<T>;
template<typename T> requires Comparable<T>
SelectionSort(vector<T>) -> SelectionSort<T>;
template<typename T, size_t SIZE> requires Comparable<T>
SelectionSort(array<T, SIZE>) -> SelectionSort<T>;
template<typename T> requires Comparable<T>
SelectionSort(T a[]) -> SelectionSort<T>;
template<typename T> requires Comparable<T>
SelectionSort(span<T>, bool reverse) -> SelectionSort<T>;
template<typename T> requires Comparable<T>
SelectionSort(vector<T>, bool reverse) -> SelectionSort<T>;
template<typename T, size_t SIZE> requires Comparable<T>
SelectionSort(array<T, SIZE>, bool reverse) -> SelectionSort<T>;
template<typename T> requires Comparable<T>
SelectionSort(T a[], bool reverse) -> SelectionSort<T>;
#endif //ALGORITHMS_SELECTIONSORT_HPP