Wednesday, 16 September 2015

Insertion Sort

Insertion Sort: The idea behind is that in each iteration, it consumes one element from the input elements, removes it and finds its correct position i.e., where it belongs in the sorted list and places it there.
It iterates the array by growing the sorted list behind it at each iteration. It checks the current element with the largest value in the sorted list. If the current element is larger, then it leaves the element at its place and moves to the next element else it finds its correct position in the sorted list and moves it to that position. It is done by shifting all the elements which are larger than the current element to one position ahead.

Since, 7 as the first element has no other element to be compared with, it remains at its position. Now when we move towards 4, we have 7 which is the largest element in the sorted list and greater than 4. So we will move 4 to its correct position. Similarly with 5, as 7 (largest element in the sorted list) is greater than 5, we will move 5 to its correct position. Finally for 2, all the elements on left side of 2 (sorted list) are moved one position forward as all are greater than 2 and then 2 is placed on first position. Finally you will get a sorted array.
Complexity : Complexity of Insertion sort is O(n2) .

import java.util.Scanner;
class Insertion
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
void input()
{
for(int i=0;i<n;i++)
{
System.out.printf("Enter the %d element:",(i+1));
arr[i]=sc.nextInt();
}
}
void sort()
{
for(int i=1;i<n;i++)
{
int j=i;
int key=arr[j];
int x=j-1;
while(key<arr[x]&&j>0)
{
arr[j]=arr[j-1];
j=j-1;
x=j-1;
if(x==-1)
break;
}
arr[j]=key;
}
}
void display()
{
System.out.println();
System.out.println("the sorted array is:");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+"\t");
}
}
void showsteps()
{
for(int i=1;i<n;i++)
{
int j=i;
int key=arr[j];
int x=j-1;
System.out.println();
System.out.printf("OuterLoop: %d",i);
while(key<arr[x]&&j>0)
{
System.out.println();
System.out.printf("InnerLoop: %d",j);
System.out.println();
arr[j]=arr[j-1];
j=j-1;
x=j-1;
for(int t=0;t<n;t++)
System.out.print(arr[t]+"\t");
System.out.println();

if(x==-1)
break;
}
arr[j]=key;
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}

}
class InsertionTest
{
public static void main(String args[])
{
System.out.println("enter the total number of elements:");
Insertion i1=new Insertion();
i1.input();
//i1.sort();
//i1.display();
i1.showsteps();
i1.display();
}
}

Friday, 11 September 2015

selection sort

Selection Sort: This algorithm is based on the idea of finding the minimum or maximum element in the unsorted array and then putting it in its correct position for a sorted array.
We have an array A [ ] = {7, 5, 4, 2} and we need to sort it in ascending order.
Let’s find the minimum element in the array i.e., 2 and then replace it with the first position's element, i.e., 7. Now we find the second largest element in the remaining unsorted array and put it at the second position and so on.


Complexity : Here as to find the minimum element from the array of n elements, we require n-1 comparisons to be performed. Then, after putting minimum element to its proper position, size of unsorted array reduces to n-1and then n-2 comparisons are required to find the minimum in the unsorted array. Therefore (n-1) + (n-2 ) + .......+ 1 = ( n * (n-1) ) / 2 comparisons and n exchanges (swapping ), which gives the complexity of O( n2 ).



import java.util.Scanner;
class Selection
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
void input()
{
for(int i=0;i<n;i++)
{
System.out.printf("enter the value of %d element",(i+1));
arr[i]=sc.nextInt();
}
}
void display()
{
System.out.println();
System.out.println("the sorted array is:");
for(int i=0;i<n;i++)
System.out.print(arr[i]+"\t");
}
void sort()
{
int minimum=0;
for(int i=0;i<n-1;i++)
{
minimum=i;
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[minimum])
{
minimum=j;
}
int temp=arr[i];
arr[i]=arr[minimum];
arr[minimum]=temp;
}
}
}
void showSteps()
{
int minimum=0;
for(int i=0;i<n-1;i++)
{
minimum=i;
System.out.println();
System.out.printf("outer loop i=%d\t",i);
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[minimum])
{
minimum=j;
}
}
int temp=arr[i];
arr[i]=arr[minimum];
arr[minimum]=temp;
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
for(int k=0;k<n;k++)
System.out.print(arr[k]+"\t");
}
}
}
class SelectionTest
{
public static void main(String args[])
{
System.out.println("enter the value of total number of elements:");
Selection s1=new Selection();
s1.input();
//s1.sort();
//s1.display();
s1.showSteps();
s1.display();
}
}











Sorting

Sorting is a process of arranging items in ascending or descending order. This process can be implemented via many different algorithms.
Bubble Sort: This algorithm is based on the idea of repeatedly comparing pairs of  elements and then switching their positions if they exist in the wrong order.
Complexity: The complexity of bubble sort is O(n2) in the worst and average case because for every element we iterate over the the entire array each time.




//Bubble Sort
import java.util.Scanner;
class Bubble
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
void input()
{
for(int i=0;i<n;i++)
{
System.out.printf("enter the value of %d element:",(i+1));
arr[i]=sc.nextInt();
}
}
void sort()
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void display()
{
System.out.println("the sorted array is:");
for(int i=0;i<n;i++)
System.out.print(arr[i]+"\t");
}
void steps()
{
for(int i=0;i<n-1;i++)
{
System.out.printf("Outer Loop i=%d\n",i);
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
System.out.printf("\t\t Inner Loop j=%d\n",j);
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
for(int k=0;k<n;k++)
{
System.out.print(arr[k]+"\t");
}
System.out.println();
}
}
}
}
class BubbleTest
{
public static void main(String args[])
{
System.out.println("enter the total number of elements:");
Bubble b1=new Bubble();
b1.input();
//b1.sort();
//b1.display();
b1.steps();
b1.display();
}
}

Wednesday, 9 September 2015


java quiz-1



1
public static void main(String[] args)
{
byte b1= 25; byte b2=45;
byte b3= b1+b2;
}
25
70
CompileError
RunTimeException


2
Consider the following command-line invocations?
i. java Arrays
ii. java Arrays 12
iii. java Arrays 12 32
class Arrays
{
public static void main(String [ ] args)
{
for(int x=0;args.length>x++;)
{
System.out.print(args[x]+ " ");
}
Only the invocation i will complete without throwing exceptions
Only Invocation i will throw an exception.
Invocation ii will produce he output 12.
Invocation iii will produce he output 12 32.


3
What is the result?
public static void main(String[] args)
{
Object obj = new int[] { 1, 2, 3 };
int[] someArray = (int[])obj; // line 13
for (int i : someArray)
System.out.print(i + " "); // line 14
}
1 2 3
Compilation fails because of an error in line 12.
Compilation fails because of an error in line 13.
Compilation fails because of an error in line 14.


4
What is the result of running the following code with "java Test debug":
class Test
{
public static void main(String [ ] args)
{
if (args.length == 1 | args[1].equals("debug"))
{
System.out.println(args[0]);
}
else
{
System.out.println("Release");
}
}
}
Debug
Release
Compilation fails
An exception is thrown at run-time


5
public class LoopTest
{
public static void main(String args[])
{
int a = 15;
outside: for (int i = 0; i<3 4="" br="" i="" line=""> inside: System.out.print(i); // line 5
for (int j = 1; j<3 br="" j=""> if (a>5)
continue inside;//line 8
break outside; // line 9
}
}
}
}
Compile Error at line 5
Runtime exception is thrown at line 8
Compile Error at line 9
Compile Error at line 8


6
public class Tester
{
public static void main(String[] args)
{
do
{
System.out.print("inside do");
} while (false);
while (false)
{
System.out.print("inside while");
}
System.out.print("outside");
}
}
inside do outside
inside do inside while outside
outside
compile error


7
which of the followings are INCORRECT when you try to compile and execute the below code ?
class s
{
public static void main(String agr[])
{
short s1=4; //LINE 1
short s2 = s1+=s1; //LINE 2
short s3= s1+s2; //LINE 3
byte b2=(byte)((byte)s1 +(byte)(byte)s2); //LINE 5
}
}
compile time error at LINE 3
compile time error at LINE 2
compile time error at LINE 1
compile time error at LINE 5


8
class Test
{
public static void main(String[] args)
{
float f = 1; // line 1
System.out.println(++f); // line 2
}
}
compilation error in line 1
compilation error in line 2
2.0
Runtime exception


9
public class Tester {
static void test(float x) {
System.out.print("float");
}
static void test(double x) {
System.out.print("double");
}
public static void main(String[] args) {
test(99.9);
}
}
float
double
Compilation error
Exception is thrown at run time


10
if byte b=50;
b=b*2;
error ! can't assign an int to a byte
b=100
b=50
b=100.0

Tuesday, 18 August 2015

Implementation of FirstFit, BestFit and WorstFit algorithms


First-fit:    Allocate the first hole that is big enough.
Best-fit:    Allocate the smallest hole that is big enough; must search entire list, unless ordered by size.Produces the smallest leftover hole.
Worst-fit:  Allocate the largest hole; must also search entire list. Produces the largest leftover hole.
                   How to satisfy a request of size n from a list of free holes.
First-fit and best-fit better than worst-fit in terms of speed and storage utilization.




import java.util.Scanner;
class Memory
{
int Nop,Noh;
int temp[];
int Psize[];
int Hsize[];
Scanner sc=new Scanner(System.in);
void inputData()
{
System.out.println("enter the total number of processes:");
Nop=sc.nextInt();
System.out.println("enter the total number of Holes");
Noh=sc.nextInt();
Psize=new int[Nop];
Hsize=new int[Noh];
System.out.println("enter the process size of each process:");
for(int i=0;i<Nop;i++)
{
Psize[i]=sc.nextInt();
}
System.out.println("enter the hole size of each hole:");
for(int i=0;i<Noh;i++)
{
Hsize[i]=sc.nextInt();
}
}
void display()
{
System.out.println("process size of each process is :");
for(int i=0;i<Nop;i++)
System.out.print(Psize[i]);
System.out.println("hole size of each hole is:");
for(int i=0;i<Noh;i++)
System.out.print(Hsize[i]);
}
void sort()
{
temp=new int[Noh];
for(int i=0;i<Noh;i++)
temp[i]=Hsize[i];
for(int i=0;i<Noh-1;i++)
{
for(int j=i+1;j<Noh;j++)
{
if(Hsize[i]>Hsize[j])
{
int temp=Hsize[i];
Hsize[i]=Hsize[j];
Hsize[j]=temp;
}
}
}
}
void FirstFit()
{
int i_freg=0,e_freg=0;
int flag[]=new int[Noh];
int j;
for(int i=0;i<Nop;i++)
{
for(j=0;j<Noh;j++)
{
if(flag[j]==0 && Hsize[j]>=Psize[i])
{
flag[j]=1;
i_freg=i_freg+Hsize[j]-Psize[i];
break;
}
}
if(j==Noh)
System.out.printf("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(int i=0;i<Noh;i++)
{
if(flag[i]==0)
e_freg=e_freg+Hsize[i];
}
System.out.format("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",i_freg);
System.out.format("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",e_freg);
}
void bestFit()
{
int i_freg=0,e_freg=0;
int flag[]=new int[Noh];
int j;

for(int i=0;i<Nop;i++)
{
for(j=0;j<Noh;j++)
{
if(flag[j]==0 && Hsize[j]>=Psize[i])
{
flag[j]=1;
i_freg=i_freg+Hsize[j]-Psize[i];
break;
}
}
if(j==Noh)
System.out.format("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(int i=0;i<Noh;i++)
{
if(flag[i]==0)
e_freg=e_freg+Hsize[i];
}
System.out.format("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",i_freg);
System.out.format("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",e_freg);

for(int i=0;i<Noh;i++)
Hsize[i]=temp[i];
}
void worstFit()
{
int i_freg=0,e_freg=0;
int flag[]=new int[Noh];
int j;
for(int i=0;i<Nop;i++)
{
for(j=Noh-1;j>=0;j--)
{
if(flag[j]==0 && Hsize[j]>=Psize[i])
{
flag[j]=1;
i_freg=i_freg+Hsize[j]-Psize[i];
break;
}
}
if(j<0)
System.out.format("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(int i=0;i<Noh;i++)
{
if(flag[i]==0)
e_freg=e_freg+Hsize[i];
}
System.out.format("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",i_freg);
System.out.format("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",e_freg);

for(int i=0;i<Noh;i++)
Hsize[i]=temp[i];
}
}
class MemoryTest
{
public static void main(String args[])
{
Memory m1=new Memory();
Scanner sc=new Scanner(System.in);
m1.inputData();
while(true)
{
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("press 1 for Best Fit:");
System.out.println("press 2 for First Fit:");
System.out.println("press 3 for Worst Fit:");
System.out.println("press 4 for Exit:");
System.out.println("Select the algorithm for memory management:");
int ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("BEST FIT ALGORITHM:");
m1.sort();
m1.bestFit();
break;

case 2: System.out.println("FIRST FIT ALGORITHM:");
m1.FirstFit();
break;

case 3: System.out.println("WORST FIT ALGORITHM:");
m1.sort();
m1.worstFit();
break;
case 4: 
System.exit(0);
default:
System.out.println("enter the right choice:");
}
}
}
}






Implementation of Priority Scheduling

Priority Based Scheduling

  • Each process is assigned a priority. Process with highest priority is to be executed first and so on.
  • Processes with same priority are executed on first come first serve basis.
  • Priority can be decided based on memory requirements, time requirements or any other resource requirement.



import java.util.Scanner;
class Priority
{
Scanner sc=new Scanner(System.in);
int MaxProcess=sc.nextInt();
int Pid[]=new int[MaxProcess];
int Burst[]=new int[MaxProcess];
int Wait[]=new int[MaxProcess];
int Trt[]=new int[MaxProcess];
int Arrival[]=new int[MaxProcess];
int Prior[]=new int[MaxProcess];
void input()
{
for(int i=0;i<MaxProcess;i++)
{
System.out.format("enter the process-id for this %d",(i+1));
Pid[i]=sc.nextInt();
System.out.format("enter the burst time for this p%d",Pid[i]);
Burst[i]=sc.nextInt();
System.out.format("enter the  priority for this p%d",Pid[i]);
Prior[i]=sc.nextInt();
System.out.format("enter the arrival time for this p%d",Pid[i]);
Arrival[i]=sc.nextInt();
}
       System.out.println("the process-id and burst time is:\n");
       System.out.println("process-id\t\tbursttime\t\tpriority\t\tarrival\n");
for(int i=0;i<MaxProcess;i++)
{
System.out.format("%d\t\t\t%d\t\t\t%d\t\t\t%d\n",Pid[i],Burst[i],Prior[i],Arrival[i]);
}
}
void turnAroundTime()
{
for(int i=0;i<MaxProcess;i++)
{
Trt[i]=Wait[i]+Burst[i];
}
}
void waitingTime()
{
int i,Total;
Wait[0]=0;
Total=Arrival[0];
for(i=1;i<MaxProcess;i++)
{
Total=Total+Burst[i-1];
Wait[i]=Total-Arrival[i];;
}
}
void displaySort()
{
System.out.println("after sorting on the basis of arrival time:");
float sum1=0,sum2=0;
System.out.println("p-id\tbursttime\tpriority\tarrival\twaittime\tturntime\n");
for(int i=0;i<MaxProcess;i++)
System.out.format("%d\t%d\t\t%d\t\t%d\t%d\t\t%d\n",Pid[i],Burst[i],Prior[i],Arrival[i],Wait[i],Trt[i]);
}
void display()
{
float sum1=0,sum2=0;
System.out.println("p-id\tbursttime\tpriority\tarrival\twaittime\tturntime\n");
for(int i=0;i<MaxProcess;i++)
System.out.format("%d\t%d\t\t%d\t\t%d\t%d\t\t%d\n",Pid[i],Burst[i],Prior[i],Arrival[i],Wait[i],Trt[i]);
for(int i=0;i<MaxProcess;i++)
{
sum1=sum1+Wait[i];
sum2=sum2+Trt[i];
}
System.out.format("waiting time is %f\n",sum1/MaxProcess);
System.out.format("totalrunaroundtime is %f\n",sum2/MaxProcess);
}

void sort()
{
for(int i=0;i<MaxProcess-1;i++)
{
  for(int j=i+1;j<MaxProcess;j++)
{
if(Prior[i]>Prior[j])
{
int temp1=Arrival[i];
Arrival[i]=Arrival[j];
Arrival[j]=temp1;

int temp2=Burst[i];
Burst[i]=Burst[j];
Burst[j]=temp2;

int temp3=Pid[i];
Pid[i]=Pid[j];
Pid[j]=temp3;
int temp4=Prior[i];
Prior[i]=Prior[j];
Prior[j]=temp4;

}
}
}
int temp=0;
int min=Arrival[0];
for(int i=1;i<MaxProcess;i++)
{
if(min>Arrival[i])
{
min=Arrival[i];
temp=i;
}
}
int f1=Arrival[temp];
int f2=Burst[temp];
int f3=Pid[temp];
int f4=Prior[temp];
for(int i=temp;i>0;i--)
{
Arrival[i]=Arrival[i-1];
Burst[i]=Burst[i-1];
Pid[i]=Pid[i-1];
}
Burst[0]=f2;
Arrival[0]=f1;
Pid[0]=f3;
Prior[0]=f4;
}
void drawGanttChart()
{

System.out.println();
for(int i=0;i<MaxProcess;i++)
{
  for(int j=0;j<Burst[i];j++)
System.out.print("|");

System.out.format(" p%d ",Pid[i]);
}
}

}
class PriorityTest
{
public static void main(String args[])
{
System.out.println("enter the total number of process:");
Priority p1=new Priority();
p1.input();
p1.sort();
p1.displaySort();
p1.waitingTime();
p1.turnAroundTime();
p1.display();
p1.drawGanttChart();
}
}

Implementation of Shortest Job First Scheduling algorithm

Shortest Job First (SJF)

  • Best approach to minimize waiting time.
  • Impossible to implement
  • Processer should know in advance how much time process will take







import java.util.Scanner;
class Sjf
{
Scanner sc=new Scanner(System.in);
int MaxProcess=sc.nextInt();
int Pid[]=new int[MaxProcess];
int Burst[]=new int[MaxProcess];
int Wait[]=new int[MaxProcess];
int Trt[]=new int[MaxProcess];
int Arrival[]=new int[MaxProcess];
void input()
{
for(int i=0;i<MaxProcess;i++)
{
System.out.format("enter the process-id for this %d",(i+1));
Pid[i]=sc.nextInt();
System.out.format("enter the burst time for this p%d",Pid[i]);
Burst[i]=sc.nextInt();
System.out.format("enter the arrival time for this p%d",Pid[i]);
Arrival[i]=sc.nextInt();
}
    System.out.println("the process-id and burst time is:\n");
    System.out.println("process-id\t\tbursttime\t\tarrival\n");
for(int i=0;i<MaxProcess;i++)
{
System.out.format("%d\t\t\t%d\t\t\t%d\n",Pid[i],Burst[i],Arrival[i]);
}
}
void turnAroundTime()
{
for(int i=0;i<MaxProcess;i++)
{
Trt[i]=Wait[i]+Burst[i];
}
}
void waitingTime()
{
int i,Total;
Wait[0]=0;
Total=Arrival[0];
for(i=1;i<MaxProcess;i++)
{
Total=Total+Burst[i-1];
Wait[i]=Total-Arrival[i];;
}
}
void displaySort()
{
System.out.println("after sorting on the basis of arrival time:");
float sum1=0,sum2=0;
System.out.println("process-id\t bursttime\t arrival \t waiting time\t turnaroundtime\n");
for(int i=0;i<MaxProcess;i++)
System.out.format("%d\t\t\t%d\t\t%d\t\t%d\t\t%d\n",Pid[i],Burst[i],Arrival[i],Wait[i],Trt[i]);
}
void display()
{
float sum1=0,sum2=0;
System.out.println("process-id\t bursttime\t arrival \t waiting time\t turnaroundtime\n");
for(int i=0;i<MaxProcess;i++)
System.out.format("%d\t\t\t%d\t\t%d\t\t%d\t\t%d\n",Pid[i],Burst[i],Arrival[i],Wait[i],Trt[i]);
for(int i=0;i<MaxProcess;i++)
{
sum1=sum1+Wait[i];
sum2=sum2+Trt[i];
}
System.out.format("waiting time is %f\n",sum1/MaxProcess);
System.out.format("totalrunaroundtime is %f\n",sum2/MaxProcess);
}

void sort()
{
for(int i=0;i<MaxProcess-1;i++)
{
  for(int j=i+1;j<MaxProcess;j++)
{
if(Burst[i]>Burst[j])
{
int temp1=Arrival[i];
Arrival[i]=Arrival[j];
Arrival[j]=temp1;

int temp2=Burst[i];
Burst[i]=Burst[j];
Burst[j]=temp2;

int temp3=Pid[i];
Pid[i]=Pid[j];
Pid[j]=temp3;

}
}
}
int temp=0;
int min=Arrival[0];
for(int i=1;i<MaxProcess;i++)
{
if(min>Arrival[i])
{
min=Arrival[i];
temp=i;
}
}
int f1=Arrival[temp];
int f2=Burst[temp];
int f3=Pid[temp];
for(int i=temp;i>0;i--)
{
Arrival[i]=Arrival[i-1];
Burst[i]=Burst[i-1];
Pid[i]=Pid[i-1];
}
Burst[0]=f2;
Arrival[0]=f1;
Pid[0]=f3;
}
void drawGanttChart()
{

System.out.println();
for(int i=0;i<MaxProcess;i++)
{
  for(int j=0;j<Burst[i];j++)
System.out.print("|");

System.out.format(" p%d ",Pid[i]);
}
}

}
class SjfTest
{
public static void main(String args[])
{
System.out.println("enter the total number of process:");
Sjf s1=new Sjf();
s1.input();
s1.sort();
s1.displaySort();
s1.waitingTime();
s1.turnAroundTime();
s1.display();
s1.drawGanttChart();
}
}



Implementation of First Come First Serve Scheduling alogrithm

       First Come First Serve (FCFS)

  • Jobs are executed on first come, first serve basis.
  • Easy to understand and implement.
  • Poor in performance as average wait time is high.




import java.util.Scanner;
class Fcfs
{
Scanner sc=new Scanner(System.in);
int MaxProcess=sc.nextInt();
int Pid[]=new int[MaxProcess];
int Burst[]=new int[MaxProcess];
int Wait[]=new int[MaxProcess];
int Trt[]=new int[MaxProcess];
int Arrival[]=new int[MaxProcess];
void input()
{
for(int i=0;i<MaxProcess;i++)
{
System.out.format("enter the process-id for this %d",(i+1));
Pid[i]=sc.nextInt();
System.out.format("enter the burst time for this p%d",Pid[i]);
Burst[i]=sc.nextInt();
System.out.format("enter the arrival time for this p%d",Pid[i]);
Arrival[i]=sc.nextInt();
}
    System.out.println("the process-id and burst time is:\n");
    System.out.println("process-id\t\tbursttime\t\tarrival\n");
for(int i=0;i<MaxProcess;i++)
{
System.out.format("%d\t\t\t%d\t\t\t%d\n",Pid[i],Burst[i],Arrival[i]);
}
}
void turnAroundTime()
{
for(int i=0;i<MaxProcess;i++)
{
Trt[i]=Wait[i]+Burst[i];
}
}
void waitingTime()
{
int i,Total;
Wait[0]=0;
Total=Arrival[0];
for(i=1;i<MaxProcess;i++)
{
Total=Total+Burst[i-1];
Wait[i]=Total-Arrival[i];;
}
}
void displaySort()
{
System.out.println("after sorting on the basis of arrival time:");
float sum1=0,sum2=0;
System.out.println("process-id\t bursttime\t arrival \t waiting time\t turnaroundtime\n");
for(int i=0;i<MaxProcess;i++)
System.out.format("%d\t\t\t%d\t\t%d\t\t%d\t\t%d\n",Pid[i],Burst[i],Arrival[i],Wait[i],Trt[i]);
}
void display()
{
float sum1=0,sum2=0;
System.out.println("process-id\t bursttime\t arrival \t waiting time\t turnaroundtime\n");
for(int i=0;i<MaxProcess;i++)
System.out.format("%d\t\t\t%d\t\t%d\t\t%d\t\t%d\n",Pid[i],Burst[i],Arrival[i],Wait[i],Trt[i]);
for(int i=0;i<MaxProcess;i++)
{
sum1=sum1+Wait[i];
sum2=sum2+Trt[i];
}
System.out.format("waiting time is %f\n",sum1/MaxProcess);
System.out.format("totalrunaroundtime is %f\n",sum2/MaxProcess);
}

void sort()
{
for(int i=0;i<MaxProcess-1;i++)
{
  for(int j=i+1;j<MaxProcess;j++)
{
if(Arrival[i]>Arrival[j])
{
int temp1=Arrival[i];
Arrival[i]=Arrival[j];
Arrival[j]=temp1;

int temp2=Burst[i];
Burst[i]=Burst[j];
Burst[j]=temp2;

int temp3=Pid[i];
Pid[i]=Pid[j];
Pid[j]=temp3;

}
}
}
}
void drawGanttChart()
{

System.out.println();
for(int i=0;i<MaxProcess;i++)
{
  for(int j=0;j<Burst[i];j++)
System.out.print("|");

System.out.format(" p%d ",Pid[i]);
}
}

}
class FcfsTest
{
public static void main(String args[])
{
System.out.println("enter the total number of process:");
Fcfs f1=new Fcfs();
f1.input();
f1.sort();
f1.displaySort();
f1.waitingTime();
f1.turnAroundTime();
f1.display();
f1.drawGanttChart();
}
}