Wednesday, October 30, 2013

TO IMPLEMENT SHORTEST JOB FIRST IN JAVA

/*
TOPIC : TO IMPLEMENT SHORTEST JOB FIRST IN JAVA
*/

import java.util.*;
public class SJF
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].btime>p[j+1].btime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
}
}
class Process {
  int id,btime,atime;
}
/* OUTPUT
Enter no of processes :
2
PROCESS ID :
99
ARRIVAL TIME :
20
Burst TIME :
12
PROCESS ID :
10
ARRIVAL TIME :
21
Burst TIME :
32
Process id      Arrival time    Burst time
...............................
99              20              12
10              21              32
*/

No comments:

Post a Comment