1
1
#include " SortController.h"
2
2
#include " Utils.h"
3
3
4
+ /* *
5
+ * @brief Sort controller constructor
6
+ *
7
+ * @param windowSize Window size vector (x, y)
8
+ * @param timeSleep Time to wait between iterations while sorting in miliseconds
9
+ */
4
10
SortController::SortController (sf::Vector2u windowSize, int timeSleep) {
5
11
this ->winWidth = windowSize.x ;
6
12
this ->winHeight = windowSize.y ;
7
13
8
14
this ->timeSleep = timeSleep;
9
15
}
10
16
11
- // /////////////////////////////
12
17
//
13
- // Vector control methods
18
+ // ──────────────────────────────────────────────────────────────────────────────────── I ──────────
19
+ // :::::: V E C T O R C O N T R O L M E T H O D S : : : : : : : :
20
+ // ──────────────────────────────────────────────────────────────────────────────────────────────
14
21
//
15
- // /////////////////////////////
16
22
23
+ /* *
24
+ * @brief Empty the array
25
+ *
26
+ */
17
27
void SortController::clear () {
18
28
sortElements.clear ();
19
29
}
20
30
31
+ /* *
32
+ * @brief Populate the array with Sorable elements
33
+ *
34
+ * @param numOfElements Number of elements to be included in the array (array size)
35
+ */
21
36
void SortController::populate (int numOfElements) {
22
37
for (int n = 0 ; n < numOfElements; n++) {
23
38
Sortable sortable (((float )winWidth / numOfElements), ((float )winHeight / numOfElements) * (n+1 ), n); // Width defined for max space in window, height defined by element value
24
39
sortElements.push_back (sortable);
25
40
}
26
41
}
27
42
43
+ /* *
44
+ * @brief Randomize element positions inside the array
45
+ *
46
+ */
28
47
void SortController::randomize () {
29
48
auto rd = std::random_device{};
30
49
auto rng = std::default_random_engine{ rd () };
31
50
std::shuffle (std::begin (sortElements), std::end (sortElements), rng);
32
51
};
33
52
53
+ /* *
54
+ * @brief Update time sleep
55
+ *
56
+ * @param t Time to wait between iterations while sorting in miliseconds
57
+ */
34
58
void SortController::setTimeSleep (int t) {
35
59
timeSleep = t;
36
60
}
37
61
62
+ /* *
63
+ * @brief Display sort info on terminal after the algorithm finished
64
+ *
65
+ * @param sortType Number associated to each sort type
66
+ * @param isSorting true = display only sort type and sorting message, false = display all info
67
+ * @param numOfComparisons Number of comparisons that the algorithm running made
68
+ * @param sortTime Time that the algorithm took to sort all elements
69
+ *
70
+ * TODO: Add number of comparisons and time while the algorithm is sorting
71
+ */
38
72
void SortController::displaySortInfo (int sortType, bool isSorting, int numOfComparisons, int sortTime) {
39
73
system (CLEAR);
40
74
@@ -51,12 +85,17 @@ void SortController::displaySortInfo(int sortType, bool isSorting, int numOfComp
51
85
}
52
86
}
53
87
54
- // /////////////////////////////
55
88
//
56
- // Sorting methods
89
+ // ────────────────────────────────────────────────────────────────────── II ──────────
90
+ // :::::: S O R T I N G M E T H O D S : : : : : : : :
91
+ // ────────────────────────────────────────────────────────────────────────────────
57
92
//
58
- // /////////////////////////////
59
93
94
+ /* *
95
+ * @brief Start sort timer, run the algorithm and increment the number of comparisons while the array isn't sorted, and display sort info at the end.
96
+ *
97
+ * @param sortType Number associated to the algorithm
98
+ */
60
99
void SortController::startSort (int sortType) {
61
100
isSorting = true ;
62
101
int numOfComparisons = 0 ;
@@ -90,6 +129,12 @@ void SortController::startSort(int sortType) {
90
129
checkSortAnim ();
91
130
}
92
131
132
+ /* *
133
+ * @brief Iterate one time through the array to check if is sorted
134
+ *
135
+ * @return true The array is sorted
136
+ * @return false The array isn't sorted
137
+ */
93
138
bool SortController::isSorted () {
94
139
for (int n = 0 ; n < sortElements.size ()-1 ; n++) {
95
140
if (sortElements[n].value > sortElements[n+1 ].value )
@@ -101,7 +146,11 @@ bool SortController::isSorted() {
101
146
return true ;
102
147
};
103
148
104
- // This function is only for the "checking animation", the verification is made by isSorted()
149
+ /* *
150
+ * @brief Do a checking animation from the start to the end of the array changing color to green to show that the algorithm finished. (Does not check the array, only do
151
+ * the color animation)
152
+ *
153
+ */
105
154
void SortController::checkSortAnim () {
106
155
for (int n = 0 ; n < sortElements.size (); n++) {
107
156
sortElements[n].color = sf::Color::Green;
0 commit comments