Skip to content

Commit 317e030

Browse files
committed
Fix: documentation issues with the functions, headers. added tests in main
1 parent 2a5e8af commit 317e030

File tree

1 file changed

+75
-24
lines changed

1 file changed

+75
-24
lines changed

greedy_algorithms/job_sequencing_algorithm.cpp

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,59 @@
6868

6969
// Implementation of the greedy algorithm using priority_queue AKA max_heap.
7070
// C++ code for the above approach
71-
#include <algorithm>
72-
#include <cassert>
73-
#include <iostream>
74-
#include <queue>
75-
#include <vector>
76-
using namespace std;
77-
78-
// A structure to represent a job
71+
#include <algorithm> // for sort function.
72+
#include <cassert> // for assert funtion in testing
73+
#include <iostream> // for standard in/out
74+
#include <queue> // for priority queue data structure
75+
#include <vector> // for vector data structure
76+
77+
/**
78+
* @namespace
79+
* @brief Greedy Algorithms
80+
*/
81+
namespace greedy_algorithms {
82+
83+
/**
84+
* @brief A structure to represent a job
85+
*/
7986
struct Job {
8087
char id; // Job Id
8188
int dead; // Deadline of job
8289
int profit; // Profit earned if job is completed before deadline
83-
};
90+
}; // namespace greedy_algorithmsstruct Job
8491

8592
// Custom sorting helper struct which is used for sorting
8693
// all jobs according to profit
94+
95+
/**
96+
* @brief Utility function that finds Custom sorting helper
97+
* struct which is used for sorting all jobs according to profit
98+
*
99+
* @param a first job struct
100+
* @param b second job struct
101+
* @returns true if the proft of a less than profit of b, else false.
102+
*/
87103
struct jobProfit {
88104
bool operator()(Job const& a, Job const& b) {
89105
return (a.profit < b.profit);
90106
}
91107
};
92108

93-
// Returns maximum profit from jobs
94-
void printJobScheduling(Job arr[], int n) {
95-
vector<Job> result;
96-
sort(arr, arr + n, [](Job a, Job b) { return a.dead < b.dead; });
109+
/**
110+
* @brief function that get the maximum profit from jobs
111+
*
112+
* @param arr array fo Jobs
113+
* @param n number of the jobs in the array
114+
* @returns vector of 'char' including the IDs of jobs
115+
* that has the maximum profit from jobs
116+
*
117+
*/
118+
std::vector<char> getJobScheduling(Job arr[], int n) {
119+
std::vector<Job> result;
120+
std::sort(arr, arr + n, [](Job a, Job b) { return a.dead < b.dead; });
97121

98122
// set a custom priority queue
99-
priority_queue<Job, vector<Job>, jobProfit> pq;
123+
std::priority_queue<Job, std::vector<Job>, jobProfit> pq;
100124

101125
for (int i = n - 1; i >= 0; i--) {
102126
int slot_available;
@@ -129,20 +153,47 @@ void printJobScheduling(Job arr[], int n) {
129153
sort(result.begin(), result.end(),
130154
[&](Job a, Job b) { return a.dead < b.dead; });
131155

132-
// print the result
133-
for (int i = 0; i < result.size(); i++) cout << result[i].id << ' ';
134-
cout << endl;
156+
std::vector<char> answer;
157+
158+
// extract the job IDs only to be returned
159+
for (int i = 0; i < result.size(); i++) answer.push_back(result[i].id);
160+
161+
// return the job IDs
162+
return answer;
135163
}
164+
} // namespace greedy_algorithms
136165

137-
// main function to test the code
138-
int main() {
139-
Job arr[] = {
166+
/**
167+
* @brief Self-test implementations
168+
* @returns void
169+
*/
170+
void tests() {
171+
greedy_algorithms::Job jobs1[] = {
140172
{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}};
173+
int n = sizeof(jobs1) / sizeof(jobs1[0]);
174+
175+
// 1st test
176+
assert(greedy_algorithms::getJobScheduling(jobs1, n)[0] == 'a');
177+
assert(greedy_algorithms::getJobScheduling(jobs1, n)[1] == 'c');
178+
assert(greedy_algorithms::getJobScheduling(jobs1, n)[2] == 'e');
141179

142-
int n = sizeof(arr) / sizeof(arr[0]);
143-
cout << "Following is maximum profit sequence of jobs \n";
180+
greedy_algorithms::Job jobs2[] = {
181+
{'x', 1, 50}, {'y', 2, 60}, {'z', 2, 20}, {'w', 3, 30}};
182+
n = sizeof(jobs2) / sizeof(jobs2[0]);
144183

145-
// Function call
146-
printJobScheduling(arr, n);
184+
// 2nd test
185+
assert(greedy_algorithms::getJobScheduling(jobs2, n)[0] == 'x');
186+
assert(greedy_algorithms::getJobScheduling(jobs2, n)[1] == 'y');
187+
assert(greedy_algorithms::getJobScheduling(jobs2, n)[2] == 'w');
188+
189+
std::cout << "All tests have successfully passed!\n";
190+
}
191+
192+
/**
193+
* @brief Main function
194+
* @returns 0 on exit
195+
*/
196+
int main() {
197+
tests();
147198
return 0;
148199
}

0 commit comments

Comments
 (0)