Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
1
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
void sort(int q[],int n)
{
  int i,j;
  for(i=0;i<n-1;i++)
   for(j=0;j<n-1-i;j++)
    if(q[j+1]<q[j])
    {
        int temp=q[j];
        q[j]=q[j+1];
        q[j+1]=temp;
    }
}
void main()
{
    int pos,size,i,n,j,seektime=0,diff;
    printf("enter the disk size");
    scanf("%d",&size);
    int min=0,max=size-1;
    printf("enter the size of queue");
    scanf("%d",&n);
    int q[n+1];
    printf("enter the queue elements");
    for(i=0;i<n;i++)
     scanf("%d",&q[i]);
    printf("enter the head position");
    scanf("%d",&q[n]);
    int head=q[n];
    sort(q,n+1); 
    for(i=0;i<=n;i++)
     if(head==q[i])
     {pos=i;
      break;}

    printf("\n enter direction :0-left,1-right: ");
    int dir;
    scanf("%d",&dir);
    if(dir==0)
    {
       for(i=pos;i>=0;i--){
        printf("%d->",q[i]);
        }
        printf("0->%d->",max);
       for(i=n;i<pos;i--){
        printf("%d->",q[i]);
        seektime=(q[pos]-0)+(max-min)+(max-q[pos+1]);
    }
    }
    if(dir==1)
    {
        for(i=pos;i<n;i++)
        printf("%d->",q[i]);
        printf("%d->0->",max);
       for(i=0;i<pos;i++)
        printf("%d->",q[i]); 
        seektime=(max-q[pos])+(max-min)+(q[pos-1]-0);
    }
    printf("seek time is= %d",seektime);
}
Leave a Comment