67
67
// Implementation of the greedy algorithm using priority_queue AKA max_heap.
68
68
// C++ code for the above approach
69
69
#include < algorithm> // for sort function.
70
+ #include < array> // for std::array
70
71
#include < cassert> // for assert funtion in testing
71
72
#include < iostream> // for standard in/out
72
73
#include < queue> // for priority queue data structure
@@ -89,7 +90,7 @@ struct Job {
89
90
90
91
/* *
91
92
* @brief Utility function that finds Custom sorting helper
92
- * struct which is used for sorting all jobs according to profit
93
+ * which is used for sorting all jobs according to profit
93
94
*
94
95
* @param a first job struct
95
96
* @param b second job struct
@@ -106,15 +107,17 @@ bool jobComparator(Job const &a, Job const &b) { return (a.profit < b.profit); }
106
107
* that has the maximum profit from jobs
107
108
*
108
109
*/
109
- std::vector<char > getJobScheduling (Job arr[], int n) {
110
+ template <std::size_t N>
111
+ std::vector<char > getJobScheduling (std::array<Job, N> &arr) {
110
112
std::vector<Job> result;
111
- std::sort (arr, arr + n, [](Job a, Job b) { return a.dead < b.dead ; });
113
+ std::sort (arr.begin (), arr.end (),
114
+ [](Job a, Job b) { return a.dead < b.dead ; });
112
115
113
116
// set a custom priority queue
114
117
std::priority_queue<Job, std::vector<Job>, decltype (&jobComparator)> pq (
115
118
jobComparator);
116
119
117
- for (int i = n - 1 ; i >= 0 ; i--) {
120
+ for (int i = N - 1 ; i >= 0 ; i--) {
118
121
int slot_available;
119
122
120
123
// we count the slots available between two jobs
@@ -160,27 +163,29 @@ std::vector<char> getJobScheduling(Job arr[], int n) {
160
163
* @returns void
161
164
*/
162
165
void tests () {
163
- greedy_algorithms::Job jobs1[] = {
164
- {' a' , 2 , 100 }, {' b' , 1 , 19 }, {' c' , 2 , 27 }, {' d' , 1 , 25 }, {' e' , 3 , 15 }};
165
- int n = sizeof (jobs1) / sizeof (jobs1[0 ]);
166
+ std::array<greedy_algorithms::Job, 5 > jobs1 = {{{' a' , 2 , 100 },
167
+ {' b' , 1 , 19 },
168
+ {' c' , 2 , 27 },
169
+ {' d' , 1 , 25 },
170
+ {' e' , 3 , 15 }}};
166
171
167
172
// 1st test
168
- assert (greedy_algorithms::getJobScheduling (jobs1, n)[0 ] == ' a' );
169
- assert (greedy_algorithms::getJobScheduling (jobs1, n)[1 ] == ' c' );
170
- assert (greedy_algorithms::getJobScheduling (jobs1, n)[2 ] == ' e' );
173
+ std::vector<char > result1 = greedy_algorithms::getJobScheduling (jobs1);
174
+ assert (result1[0 ] == ' a' );
175
+ assert (result1[1 ] == ' c' );
176
+ assert (result1[2 ] == ' e' );
171
177
172
- greedy_algorithms::Job jobs2[] = {
173
- {' x' , 1 , 50 }, {' y' , 2 , 60 }, {' z' , 2 , 20 }, {' w' , 3 , 30 }};
174
- n = sizeof (jobs2) / sizeof (jobs2[0 ]);
178
+ std::array<greedy_algorithms::Job, 4 > jobs2 = {
179
+ {{' x' , 1 , 50 }, {' y' , 2 , 60 }, {' z' , 2 , 20 }, {' w' , 3 , 30 }}};
175
180
176
181
// 2nd test
177
- assert (greedy_algorithms::getJobScheduling (jobs2, n)[0 ] == ' x' );
178
- assert (greedy_algorithms::getJobScheduling (jobs2, n)[1 ] == ' y' );
179
- assert (greedy_algorithms::getJobScheduling (jobs2, n)[2 ] == ' w' );
182
+ std::vector<char > result2 = greedy_algorithms::getJobScheduling (jobs2);
183
+ assert (result2[0 ] == ' x' );
184
+ assert (result2[1 ] == ' y' );
185
+ assert (result2[2 ] == ' w' );
180
186
181
187
std::cout << " All tests have successfully passed!\n " ;
182
188
}
183
-
184
189
/* *
185
190
* @brief Main function
186
191
* @returns 0 on exit
0 commit comments