-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgorithms.h
331 lines (300 loc) · 11.7 KB
/
Algorithms.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// Created by Tyler Rolfe on 7/11/21.
//
#ifndef P3_ALGORITHMS_H
#define P3_ALGORITHMS_H
#include "Helpers.h"
#include <chrono>
using namespace std::chrono;
typedef high_resolution_clock Clock;
enum Sequence { Sedgewick, Hibbard };
/// Shell Sort
/// \param arr - The values being sorted
/// \param size - The number of elements in arr
/// \param seq - The Sequence used for the gap to sort
/// \param type - The data type the user requested -- See the enum type DataType for options
void shellSort(Sample arr[], int size, Sequence seq, DataType type) {
auto t1 = Clock::now();
vector<int> sequence;
switch (seq) {
case Sedgewick: sequence = generateSedgewickSequence(size);
case Hibbard: sequence = generateHibbardSequence(size);
}
for (auto& gap: sequence) {
for (int i = gap; i < size; i++) {
Sample temp = arr[i];
int j;
switch (type) {
case Latitude: for (j = i; j >= gap && arr[j - gap].latitude > temp.latitude; j -= gap) { arr[j] = arr[j - gap]; } break;
case Longitude: for (j = i; j >= gap && arr[j - gap].longitude > temp.longitude; j -= gap) { arr[j] = arr[j - gap]; } break;
case WaterDepth: for (j = i; j >= gap && arr[j - gap].waterDepth > temp.waterDepth; j -= gap) { arr[j] = arr[j - gap]; } break;
case OBSVNTop: for (j = i; j >= gap && arr[j - gap].obsvnTop > temp.obsvnTop; j -= gap) { arr[j] = arr[j - gap]; } break;
case OBSVNBot: for (j = i; j >= gap && arr[j - gap].obsvnBot > temp.obsvnBot; j -= gap) { arr[j] = arr[j - gap]; } break;
case Gravel: for (j = i; j >= gap && arr[j - gap].gravel > temp.gravel; j -= gap) { arr[j] = arr[j - gap]; } break;
case Sand: for (j = i; j >= gap && arr[j - gap].sand > temp.sand; j -= gap) { arr[j] = arr[j - gap]; } break;
case Mud: for (j = i; j >= gap && arr[j - gap].mud > temp.mud; j -= gap) { arr[j] = arr[j - gap]; } break;
case Clay: for (j = i; j >= gap && arr[j - gap].clay > temp.clay; j -= gap) { arr[j] = arr[j - gap]; } break;
case GrainSize: for (j = i; j >= gap && arr[j - gap].grainSize > temp.grainSize; j -= gap) { arr[j] = arr[j - gap]; } break;
case Sorting: for (j = i; j >= gap && arr[j - gap].sorting > temp.sorting; j -= gap) { arr[j] = arr[j - gap]; } break;
case OrgCarbn: for (j = i; j >= gap && arr[j - gap].orgCarbn > temp.orgCarbn; j -= gap) { arr[j] = arr[j - gap]; } break;
case Porosity: for (j = i; j >= gap && arr[j - gap].porosity > temp.porosity; j -= gap) { arr[j] = arr[j - gap]; } break;
default: break;
}
arr[j] = temp;
}
}
auto t2 = Clock::now();
switch (seq) {
case Hibbard: cout << "The Shell sort via Hibbard sequence took " << duration_cast<seconds>(t2 - t1).count() << " seconds to run." << endl; break;
case Sedgewick: cout << "The Shell sort via Sedgewick sequence took " << duration_cast<seconds>(t2 - t1).count() << " seconds to run." << endl; break;
}
}
/// Partitions the array used in Quick Sort based off the first elemet in the array
int PartitionArray(Sample arr[], int startPoint, int endPoint, DataType type) {
unsigned int i = startPoint;
float partitionPoint;
float arrUp;
float arrDown;
int up;
int down;
partitionPoint = ArrayType(arr, i, type);
up = i + 1;
down = endPoint;
arrUp = ArrayType(arr, up, type);
arrDown = ArrayType(arr, down, type);
while (up < down) {
while (arrUp <= partitionPoint && up < endPoint) {
up++;
arrUp = ArrayType(arr, up, type);
}
while (arrDown >= partitionPoint && down > startPoint) {
down--;
arrDown = ArrayType(arr, down, type);
}
if (up < down) {
swap(arr[up], arr[down]);
arrUp = ArrayType(arr, up, type);
arrDown = ArrayType(arr, down, type);
}
}
if (arrDown <= partitionPoint && down != i) {
swap(arr[i], arr[down]);
return down;
}
return i;
}
//Quick Sort
/// \param arr - The values being sorted
/// \param size - The number of elements in arr
/// \param startingPoint - the starting point of the section actively being sorted
/// \param endPoint - the end point of the section actively being sorted
/// \param type - The data type the user requested -- See the enum type DataType for options
void QuickSort(Sample arr[], int startingPoint, int endPoint, DataType type) {
int partitionPoint;
if (startingPoint < endPoint) {
partitionPoint = PartitionArray(arr, startingPoint, endPoint, type); //Clay
QuickSort(arr, startingPoint, partitionPoint - 1, type);
QuickSort(arr, partitionPoint + 1, endPoint, type);
}
}
void merge(Sample samples[], int left, int middle, int right, DataType& type) {
int subArrOne = middle - left + 1;
int subArrTwo = right - middle;
auto leftHalf = new Sample[subArrOne];
auto rightHalf = new Sample[subArrTwo];
// Copy items to temp arr
for (int i = 0; i < subArrOne; i++) {
leftHalf[i] = samples[left + i];
}
for (int j = 0; j < subArrTwo; j++) {
rightHalf[j] = samples[middle + 1 + j];
}
int indexOne = 0;
int indexTwo = 0;
int indexMerged = left;
// Merge temp arr back
while (indexOne < subArrOne && indexTwo < subArrTwo) {
switch (type) {
case Latitude: {
if (leftHalf[indexOne].latitude <= rightHalf[indexTwo].latitude) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Longitude: {
if (leftHalf[indexOne].longitude <= rightHalf[indexTwo].longitude) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case WaterDepth: {
if (leftHalf[indexOne].waterDepth <= rightHalf[indexTwo].waterDepth) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case OBSVNTop: {
if (leftHalf[indexOne].obsvnTop <= rightHalf[indexTwo].obsvnTop) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case OBSVNBot: {
if (leftHalf[indexOne].obsvnBot <= rightHalf[indexTwo].obsvnBot) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Gravel: {
if (leftHalf[indexOne].gravel <= rightHalf[indexTwo].gravel) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Sand: {
if (leftHalf[indexOne].sand <= rightHalf[indexTwo].sand) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Mud: {
if (leftHalf[indexOne].mud <= rightHalf[indexTwo].mud) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Clay: {
if (leftHalf[indexOne].clay <= rightHalf[indexTwo].clay) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case GrainSize: {
if (leftHalf[indexOne].grainSize <= rightHalf[indexTwo].grainSize) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Sorting: {
if (leftHalf[indexOne].sorting <= rightHalf[indexTwo].sorting) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case OrgCarbn: {
if (leftHalf[indexOne].orgCarbn <= rightHalf[indexTwo].orgCarbn) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
case Porosity: {
if (leftHalf[indexOne].porosity <= rightHalf[indexTwo].porosity) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
}
else {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
}
break;
}
} // end switch
indexMerged++;
}
// Copy what's left into left arr
while (indexOne < subArrOne) {
samples[indexMerged] = leftHalf[indexOne];
indexOne++;
indexMerged++;
}
// Copy what's left into right arr
while (indexTwo < subArrTwo) {
samples[indexMerged] = rightHalf[indexTwo];
indexTwo++;
indexMerged++;
}
}
void mergeSort(Sample samples[], int front, int back, DataType& type) {
if (front >= back) {
return;
}
int middle = front + (back - front) / 2;
mergeSort(samples, front, middle, type);
mergeSort(samples, middle + 1, back, type);
merge(samples, front, middle, back, type);
}
// Binary Search implementation on a sorted array
int Search(Sample arr[], int starting, int ending, float x, DataType type) {
if (ending >= starting) {
int mid = starting + (ending - starting) / 2;
if (ArrayType(arr, mid, type) == x) {
return mid; // the index of the item in the array
}
if (ArrayType(arr, mid, type) > x) {
return Search(arr, starting, mid - 1, x, type);
}
return Search(arr, mid + 1, ending, x, type);
}
return -99;
}
#endif //P3_ALGORITHMS_H