forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobSequencing.java
130 lines (124 loc) · 3.07 KB
/
jobSequencing.java
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
// This is Job Sequencing Problem using Greedy Method
import java.util.*;
public class jobSequencing
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of Jobs");
int n=sc.nextInt();
String a[]=new String[n];
int b[]=new int[n];
int c[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the Jobs");
a[i]=sc.next();
System.out.println("Enter the Profit");
b[i]=sc.nextInt();
System.out.println("Enter the DeadLine");
c[i]=sc.nextInt();
}
System.out.println("\n--Arranged Order--\n"); // printing taken input
System.out.print("Jobs: ");
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Profit: ");
for(int i=0;i<n;i++)
{
System.out.print(b[i]+" ");
}
System.out.println();
System.out.print("DeadLine:");
for(int i=0;i<n;i++)
{
System.out.print(c[i]+" ");
}
for(int i=0;i<n-1;i++) // sorting
{
for(int j=i+1;j<n;j++)
{
if(b[i]<b[j])
{
int temp=b[i]; // sorting profit
b[i]=b[j];
b[j]=temp;
temp=c[i]; // sorting deadline
c[i]=c[j];
c[j]=temp;
String temp1=a[i]; // sorting job
a[i]=a[j];
a[j]=temp1;
}
}
}
System.out.println();
System.out.println("\n--Sorted Order--\n"); // printing sorted order
System.out.print("Jobs: ");
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Profit: ");
for(int i=0;i<n;i++)
{
System.out.print(b[i]+" ");
}
System.out.println();
System.out.print("DeadLine:");
for(int i=0;i<n;i++)
{
System.out.print(c[i]+" ");
}
System.out.println();
int max=c[0]; // calculating max deadline
for(int i=0;i<n;i++)
{
if(c[i]>max)
{
max=c[i];
}
}
String x[]=new String[max];
int m[]=new int[max];
int profit=0;
for(int i=0;i<n;i++)
{
int k=c[i];
k=k-1;
if(x[k]==null )
{
x[k]=a[i];
profit+=b[i];
}
else
{
while(k!=-1)
{
if(x[k]==null)
{
x[k]=a[i];
profit+=b[i];
break;
}
k=k-1;
}
}
}
System.out.println();
System.out.println("Following is the Maximum Profit Sequence of Jobs"); // printing sequence of jobs
for(int i=0;i<max;i++)
{
System.out.print(x[i]);
if(i<max-1) {
System.out.print("-->");
}
}
System.out.println("\n");
System.out.print("Profit Earned "+profit); // printing total profit
}
}