-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJob_scheduling.c
61 lines (61 loc) · 1.71 KB
/
Job_scheduling.c
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_JOBS 10
struct Job {
short id;
short deadline;
short profit;
};
void jobSequencing(struct Job jobs[], short n) {
for (short i = 0; i < n - 1; i++) {
for (short j = 0; j < n - i - 1; j++) {
if (jobs[j].profit < jobs[j + 1].profit) {
struct Job temp = jobs[j];
jobs[j] = jobs[j + 1];
jobs[j + 1] = temp;
}
}
}
short maxDeadline = 0;
for (short i = 0; i < n; i++) {
if (jobs[i].deadline > maxDeadline) {
maxDeadline = jobs[i].deadline;
}
}
short slots[MAX_JOBS] = { 0 };
short selectedJobs[MAX_JOBS] = { 0 };
short selectedCount = 0;
for (short i = 0; i < n; i++) {
short deadline = jobs[i].deadline;
for (short j = deadline - 1; j >= 0; j--) {
if (slots[j] == 0) {
slots[j] = 1;
selectedJobs[j] = jobs[i].id;
selectedCount++;
break;
}
}
if (selectedCount == maxDeadline) {
break;
}
}
printf("Selected Jobs: ");
for (short i = 0; i < maxDeadline; i++) {
if (selectedJobs[i] != 0) {
printf("%d ", selectedJobs[i]);
}
}
}
int main() {
struct Job jobs[MAX_JOBS];
short n;
printf("Enter the number of jobs: ");
scanf("%hd", &n);
printf("Enter job details (id, deadline, profit):\n");
for (short i = 0; i < n; i++) {
scanf("%hd %hd %hd", &jobs[i].id, &jobs[i].deadline, &jobs[i].profit);
}
jobSequencing(jobs, n);
return 0;
}
//complexity : O(n log n)