- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathActivitySelection.java
More file actions
Latest commit
51 lines (51 loc) · 1.43 KB
/
Copy pathActivitySelection.java
File metadata and controls
51 lines (51 loc) · 1.43 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
importjava.io.*;
importjava.util.*;
// Driver class
// Java program for activity selection problem
// when input activities may not be sorted.
publicclassActivitySelection{
publicstaticvoidmain(Stringargs[]){
Scannersc=newScanner(System.in);
intn,i,j;
System.out.println("Enter no. of activities");
n=sc.nextInt();
intstart[]=newint[n];
intfinish[]=newint[n];
System.out.println("Enter THE START and FINISH time for activities:");
for(i=0;i<n;i++)
{
start[i]=sc.nextInt();
finish[i]=sc.nextInt();
}
//Sorting the activites according to finish time
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(finish[i]>finish[j])
{
inttemp=finish[i];//Swapping finish time
finish[i]=finish[j];
finish[j]=temp;
inttemp1=start[i];//Swapping start time
start[i]=start[j];
start[j]=temp1;
}
}
}
System.out.println("FINAL-EXECUTION-CHART");
System.out.print("("+start[0]+" "+finish[0]+")");
intx=0;
for(i=1;i<n;i++)
{
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if(start[i]>=finish[x])
{
System.out.print("("+start[i]+" "+finish[i]+")");
x=i;
}
}
}
}