-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathActivitySelection.java
51 lines (51 loc) · 1.43 KB
/
ActivitySelection.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
import java.io.*;
import java.util.*;
// Driver class
// Java program for activity selection problem
// when input activities may not be sorted.
public class ActivitySelection{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n,i,j;
System.out.println("Enter no. of activities");
n=sc.nextInt();
int start[]=new int[n];
int finish[]=new int[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])
{
int temp=finish[i];//Swapping finish time
finish[i]=finish[j];
finish[j]=temp;
int temp1=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]+")");
int x=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;
}
}
}
}