Saturday, 28 October 2017

sstf.java (OS)

import java.util.Scanner;

public class sstf
{
public static void main(String[] args)
{
int b[]=new int[20];
int a[]=new int[20],d,n,i,j,temp,s,k=0,x=0,t=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter head pointer position:");
a[0]= sc.nextInt();
System.out.println("\nEnter number of processes:");
n= sc.nextInt();
System.out.println("\nEnter processes in request order");
for(i=1;i<=n;i++)
{
a[i]=sc.nextInt();
}
b[k++]=a[0];
for(i=0;i<n;i++)
{
s=1000;
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
d=a[i]-a[j];
else
d=a[j]-a[i];
if(d<s)
{
s=d;
x=j;
}
}
t+=s;
temp=a[i+1];
a[i+1]=a[x];
a[x]=temp;
b[k++]=a[i+1];
}
System.out.println("\nProcessing order:");
for(i=1;i<=n;i++)
System.out.print("\t"+b[i]);
System.out.println("\nTotal Head Movement:"+t);
}
}

Output:

Enter head pointer position:
50

Enter number of processes:
5

Enter processes in request order
75
82
61
10
26

Processing order:
        61      75      82      26      10
Total Head Movement:104

No comments:

Post a Comment