-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.java
More file actions
169 lines (99 loc) · 2.95 KB
/
Job.java
File metadata and controls
169 lines (99 loc) · 2.95 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.*;
class job
{
int p; //.............for profit of a job
int d; //.............for deadline of a job
int v; //.............for checking if that job has been selected
job ()
{
p = 0;
d = 0;
v = 0;
}
job (int x, int y, int z) // parameterised constructor
{
p = x;
d = y;
v = z;
}}
class js
{
static int n;
static int out (job jb[], int x)
{
for (int i = 0; i < n; ++i)
if (jb[i].p == x)
return i;
return 0;
}
public static void main (String args[])
{
Scanner scr = new Scanner (System.in);
System.out.println ("Enter the number of jobs");
n = scr.nextInt ();
int max = 0; // this is to find the maximum deadline
job jb[] = new job[n];
/***********************Accepting job from user*************************/
for (int i = 0; i < n; ++i)
{
System.out.println ("Enter profit and deadline(p d)");
int p = scr.nextInt ();
int d = scr.nextInt ();
if (max < d)
max = d; // assign maximum value of deadline to "max" variable
jb[i] = new job (p, d, 0); //zero as third parameter to mark that initially it is unvisited
}
//accepted jobs from user
/***************Sorting in increasing order of deadlines*********************/
for (int i = 0; i <= n - 2; ++i)
{
for (int j = i; j <= n - 1; ++j)
{
if (jb[i].d > jb[j].d)
{
job temp = jb[i];
jb[i] = jb[j];
jb[j] = temp;
}
}
}
// sorting process ends
/*************************Displaying the jobs to the user*********************/
System.out.println ("The jobs are as follows ");
for (int i = 0; i < n; ++i)
System.out.println ("Job " + i + " Profit = " + jb[i].p +
" Deadline = " + jb[i].d);
// jobs displayed to the user
int count;
int hold[] = new int[max];
for (int i = 0; i < max; ++i)
hold[i] = 0;
/***********************Process of job sequencing begins*************************/
for (int i = 0; i < n; ++i)
{
count = 0;
for (int j = 0; j < n; ++j)
{
if (count < jb[j].d && jb[j].v == 0 && count < max
&& jb[j].p > hold[count])
{
int ch = 0;
if (hold[count] != 0)
{
ch = out (jb, hold[count]);
jb[ch].v = 0;
}
hold[count] = jb[j].p;
jb[j].v = 1;
++count;
} // end of if
} //end of inner for
} // end of outer for
/**********************job sequencing process ends****************************/
/************************calculating max profit********************************/
int profit = 0;
for (int i = 0; i < max; ++i)
profit += hold[i];
System.out.println ("The maximum profit is " + profit);
} //end main method
} //end class