Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views2 pages

Untitled Document

The document contains a C program that implements a job sequencing algorithm to maximize profit while considering job deadlines. It defines a Job structure and sorts jobs based on profit, scheduling them within their deadlines. The output displays the scheduled jobs and the total profit achieved.

Uploaded by

sumitraray2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Untitled Document

The document contains a C program that implements a job sequencing algorithm to maximize profit while considering job deadlines. It defines a Job structure and sorts jobs based on profit, scheduling them within their deadlines. The output displays the scheduled jobs and the total profit achieved.

Uploaded by

sumitraray2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Implemented Code:-

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int id;
int deadline;
int profit;
} Job;

int compare(const void* a, const void* b) {


return ((Job*)b)->profit - ((Job*)a)->profit;
}

int max(int a, int b) {


return (a > b) ? a : b;
}

void jobSequencing(Job jobs[], int n) {


qsort(jobs, n, sizeof(Job), compare);

int maxDeadline = 0;
for (int i = 0; i < n; i++)
if (jobs[i].deadline > maxDeadline)
maxDeadline = jobs[i].deadline;

int schedule[maxDeadline];
for (int i = 0; i < maxDeadline; i++)
schedule[i] = -1;

int totalProfit = 0, jobsCount = 0;

for (int i = 0; i < n; i++) {


for (int j = jobs[i].deadline - 1; j >= 0; j--) {
if (schedule[j] == -1) {
schedule[j] = jobs[i].id;
totalProfit += jobs[i].profit;
jobsCount++;
break;
}
}
}
printf("Scheduled Jobs: ");
for (int i = 0; i < maxDeadline; i++)
if (schedule[i] != -1)
printf("%d ", schedule[i]);

printf("\nTotal Profit: %d\n", totalProfit);


}

int main() {
Job jobs[] = {{1, 2, 100}, {2, 1, 50}, {3, 2, 10}, {4, 1, 20}, {5, 3, 200}};
int n = sizeof(jobs) / sizeof(jobs[0]);
jobSequencing(jobs, n);
return 0;
}

Output:-
Job Sequencing with Deadlines:
Scheduled Jobs: 5 1 3
Total Profit: 310

You might also like