-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathJobSequencing
84 lines (81 loc) · 1.64 KB
/
JobSequencing
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
finalResult(int j[],int d[],int n,int store[],int max)
{
for(int i=0;i<max;i++)
{
int min;
if(n<d[i])
min=n;
else
min=d[i];
for(int k=min-1;k>=0;k--)
{
if(store[k]==0)
{
store[k]=j[i];
break;
}
}
}
printf("The job sequence for maximum profit is :");
for(int i=0;i<max;i++)
printf("%d ",store[i]);
}
int max_deadline(int d[],int n)
{
int max;
int g=0;
for(int i=0;i<n;i++)
{
if(d[i]>g)
g=d[i];
}
if(n>g)
max=g;
else
max=n;
printf("The maximum number of jobs that can be completed is %d\n",max);
return max;
}
sort(int p[],int j[],int d[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int k=0;k<n-i-1;k++)
{
if(p[k]<p[k+1])
{
int temp1=p[k];
p[k]=p[k+1];
p[k+1]=temp1;
int temp2=j[k];
j[k]=j[k+1];
j[k+1]=temp2;
int temp3=d[k];
d[k]=d[k+1];
d[k+1]=temp3;
}
}
}
}
int main()
{
int n,i;
printf("Enter number of jobs ");
scanf("%d",&n);
int j[n],p[n],d[n];
for(int i=0;i<n;i++)
{
printf("enter deadline and profit of job %d ",i+1);
j[i]=i+1;
scanf("%d",&d[i]);
scanf("%d",&p[i]);
}
sort(p,j,d,n);
int store[n];
for(i=0;i<n;i++)
store[i]=0;
int max=max_deadline(d,n);
finalResult(j,d,n,store,max);
return 0;
}