Welcome to my blog

Wednesday 29 November 2017

10 a. IMPLEMENTATION OF ACTIVITY SELECTION



10 a. IMPLEMENTATION OF ACTIVITY SELECTION

AIM:
      To write a java program to implement Activity Selection.


ALGORITHM:
  1.Sort the activities as per finishing time in ascending order
  2.Select the first activity
  3.Select the new activity if its starting time is greater than or equal to the previously selected        activity
   4.REPEAT step 3 till all activities are checked


PROGRAM :
import java.util.*;
import java.lang.*;
import java.io.*;
class ActivitySelection
{
public static void printMaxActivities(int s[], int f[], int n)
{
int i, j;
System.out.print("Following activities are selected : n");
i = 0;
System.out.print(i+" ");
for (j = 1; j < n; j++)
{
if (s[j] >= f[i])
{
System.out.print(j+" ");
i = j;
}
}
}
public static void main(String[] args)
{
int s[] =  {1, 3, 0, 5, 8, 5};
int f[] =  {2, 4, 6, 7, 9, 9};
int n = s.length;
printMaxActivities(s, f, n);
}