-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellSort.hpp
122 lines (105 loc) · 3.46 KB
/
ShellSort.hpp
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
#ifndef ALGORITHMS_SHELLSORT_HPP
#define ALGORITHMS_SHELLSORT_HPP
#include <span>
#include "Comparable.hpp"
using namespace std;
/**
* The {@code ShellSort} class use shell-sort to sort
* a container through invoking its constructor
* with the container variable and with
* Knuth's increment sequence
* (1, 4, 13, 40, ...). In the worst case, this implementation makes
* Θ(n^(3/2)) compares and exchanges to sort
* an array of length n.
*
* This sorting algorithm is not stable.
* It uses Θ(1) extra memory (not including the input array).
*
* @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/Shell.java.html.
*
* @param <T> the generic type of an item in this sorting algorithm
*/
template<typename T> requires Comparable<T>
class ShellSort {
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 ShellSort<T>(span<T> a, bool reverse = false) {
int n = a.size();
int h = 1;
while (h < n / 3)
h = 3 * h + 1;
if (!reverse) {
while (h >= 1) {
for (int i = h; i < n; i++) {
for (int j = i; j >= h && (a[j] < a[j - h]); j -= h) {
exch(a, j, j - h);
}
}
assert(isHsorted(a, h, reverse));
h /= 3;
}
} else {
while (h >= 1) {
for (int i = h; i < n; i++) {
for (int j = i; j >= h && (a[j] > a[j - h]); j -= h) {
exch(a, j, j - h);
}
}
assert(isHsorted(a, h, reverse));
h /= 3;
}
}
};
private:
void exch(span<T> a, int i, int j);
bool isHsorted(span<T> a, int h, bool reverse);
};
template<typename T>
requires Comparable<T>
void ShellSort<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 ShellSort<T>::isHsorted(span<T> a, int h, bool reverse) {
int length = a.size();
if (!reverse) {
for (int i = h; i < length; i++)
if (a[i] < a[i - h]) return false;
return true;
} else {
for (int i = h; i < length; i++)
if (a[i] > a[i - h]) return false;
return true;
}
}
template<typename T> requires Comparable<T>
ShellSort(span<T>) -> ShellSort<T>;
template<typename T> requires Comparable<T>
ShellSort(vector<T>) -> ShellSort<T>;
template<typename T, size_t SIZE> requires Comparable<T>
ShellSort(array<T, SIZE>) -> ShellSort<T>;
template<typename T> requires Comparable<T>
ShellSort(T a[]) -> ShellSort<T>;
template<typename T> requires Comparable<T>
ShellSort(span<T>, bool reverse) -> ShellSort<T>;
template<typename T> requires Comparable<T>
ShellSort(vector<T>, bool reverse) -> ShellSort<T>;
template<typename T, size_t SIZE> requires Comparable<T>
ShellSort(array<T, SIZE>, bool reverse) -> ShellSort<T>;
template<typename T> requires Comparable<T>
ShellSort(T a[], bool reverse) -> ShellSort<T>;
#endif //ALGORITHMS_SHELLSORT_HPP