-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobsequenncinggreedymethode.c
More file actions
65 lines (50 loc) · 1.38 KB
/
jobsequenncinggreedymethode.c
File metadata and controls
65 lines (50 loc) · 1.38 KB
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
60
61
62
63
#include <stdio.h>
struct Job {
int id;
int deadline;
int profit;
};
int main() {
int n;
printf("Enter number of jobs: ");
scanf("%d", &n);
struct Job jobs[n];
for(int i = 0; i < n; i++) {
printf("Enter job id, deadline and profit: ");
scanf("%d %d %d", &jobs[i].id, &jobs[i].deadline, &jobs[i].profit);
}
// Sort jobs in descending order of profit
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(jobs[i].profit < jobs[j].profit) {
struct Job temp = jobs[i];
jobs[i] = jobs[j];
jobs[j] = temp;
}
}
}
int maxDeadline = 0;
for(int i = 0; i < n; i++)
if(jobs[i].deadline > maxDeadline)
maxDeadline = jobs[i].deadline;
int slot[maxDeadline];
for(int i = 0; i < maxDeadline; i++)
slot[i] = -1;
int totalProfit = 0;
for(int i = 0; i < n; i++) {
for(int j = jobs[i].deadline - 1; j >= 0; j--) {
if(slot[j] == -1) {
slot[j] = jobs[i].id;
totalProfit += jobs[i].profit;
break;
}
}
}
printf("\nJob sequence: ");
for(int i = 0; i < maxDeadline; i++) {
if(slot[i] != -1)
printf("J%d ", slot[i]);
}
printf("\nTotal Profit = %d\n", totalProfit);
return 0;
}