Skip to content

Commit cce0548

Browse files
authored
Job sequencing algorithm in C++
1 parent 5613d10 commit cce0548

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

C++/job_sequencing.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
5+
6+
struct Job
7+
{
8+
char id;
9+
int dead;
10+
int profit;
11+
};
12+
13+
14+
bool comparison(Job a, Job b)
15+
{
16+
return (a.profit > b.profit);
17+
}
18+
19+
20+
void printJobScheduling(Job arr[], int n)
21+
{
22+
23+
sort(arr, arr+n, comparison);
24+
25+
int result[n];
26+
bool slot[n];
27+
28+
29+
for (int i=0; i<n; i++)
30+
slot[i] = false;
31+
32+
33+
for (int i=0; i<n; i++)
34+
{
35+
36+
for (int j=min(n, arr[i].dead)-1; j>=0; j--)
37+
{
38+
39+
if (slot[j]==false)
40+
{
41+
result[j] = i;
42+
slot[j] = true;
43+
break;
44+
}
45+
}
46+
}
47+
48+
49+
for (int i=0; i<n; i++)
50+
if (slot[i])
51+
cout << arr[result[i]].id << " ";
52+
}
53+
54+
55+
int main()
56+
{
57+
Job arr[] = { {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
58+
{'d', 1, 25}, {'e', 3, 15}};
59+
int n = sizeof(arr)/sizeof(arr[0]);
60+
cout << "Following is maximum profit sequence of jobsn";
61+
printJobScheduling(arr, n);
62+
return 0;
63+
}

0 commit comments

Comments
 (0)