-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSorter.cpp
59 lines (51 loc) · 1.77 KB
/
Sorter.cpp
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
/*Written by Jacob Smith
* Allows arrays to be sorted for line calibration
Jan 19 2019*/
//include the libraries necessary to make this one work
#include "Sorter.h"
//this should work on all boards, so there is no preprocessor directive here
//creates a new Sorter object
Sorter::Sorter(int* regionMaxes ,int* regionMins, String* regions,int arrSize ) {
this->regionMaxes=regionMaxes;
this->regionMins=regionMins;
this->regions=regions;
this->arrSize=arrSize;
}
//sorts the maxes array, and then the min and regions arrrays to match
void Sorter::selectionSort() {
//goes through whole array, putting the minimum value in the start
for (int i = 0; i < arrSize - 1; i++) {
//start the minumum as the first element
int min = *(regionMaxes+i);
int minIndex = i;
//search rest of array for a smaller number
for (int j = i + 1; j < arrSize; j++) {
int num = *(regionMaxes+j);
//if number is less than unning min, it is new running min
if (num < min) {
min = num;
minIndex = j;
}
}
//swap the first element with the minimum
swap(minIndex, i, regionMaxes);
//swap for the min array
swap(minIndex, i, regionMins);
//sap for the regions array
swap(minIndex, i, regions);
}
}
//swap two elements in an array of integer
void Sorter::swap(int minIndex, int nonMinIndex, int* nums) {
//swap the two elements in the maxes array
int temp = *(nums+minIndex);
*(nums+minIndex)= *(nums+nonMinIndex);
*(nums+nonMinIndex) = temp;
}
//swap two elements in an array of strings
void Sorter::swap(int minIndex, int nonMinIndex, String* regions) {
//swap the two elements in the maxes array
String temp = *(regions+minIndex);
*(regions+minIndex) = *(regions+nonMinIndex);
*(regions+nonMinIndex) = temp;
}