操作系统scan算法与SSTF算法

Scan

package four;

import java.util.Arrays;
import java.util.Scanner;

public class Scan {
    public void clockwise(Array longth,int begin,int temp,int o){
        int []a = new int[longth.lo.length];
        for(int i=0;i<longth.lo.length;i++)
            a[i]=longth.lo[i]-begin;
        int b[]=new int[longth.lo.length];
        for(int i=0;i<a.length;i++)
            b[i]=a[i];
        Arrays.sort(a);
        
            for(int j=0;j<longth.lo.length;j++)
                if(a[longth.lo.length-temp+o]==b[j]&&longth.use[j]==false){
                    System.out.println(longth.lo[j]);                    
                    longth.use[j]=true;
                }
        
    }
    public void anticlockwise(Array longth,int begin,int temp,int o) {
        int []a = new int[longth.lo.length];
        for(int i=0;i<longth.lo.length;i++)
            a[i]=longth.lo[i]-begin;
        int b[]=new int[longth.lo.length];
        for(int i=0;i<a.length;i++)
            b[i]=a[i];
        Arrays.sort(a);
        
            for(int j=0;j<longth.lo.length;j++)
                if(a[longth.lo.length-temp-o]==b[j]&&longth.use[j]==false){
                    System.out.println(longth.lo[j]);                    
                    longth.use[j]=true;
                }
    
    }
    public static void main(String[] args) {
        int start;
        System.out.println("输入读写头");
        Scanner in=new Scanner(System.in);
        start=in.nextInt();
        System.out.println("输入序列长度");
        int amount;
        amount=in.nextInt();
        System.out.println("输入是顺时针还是逆时针1表示顺,2表示逆");
        int te=in.nextInt();
        
        
        Array a=new Array();
        System.out.println("输入序列");
        int []lon=new int[amount];
        boolean []using=new boolean[amount];
        for(int i=0;i<amount;i++){
            lon[i]=in.nextInt();
            using[i]=false;
        }
        a.setA(lon);
        a.setUse(using);
        Scan b=new Scan();
        int temp=0;
        for(int i=0;i<amount;i++)
            if(a.lo[i]>=start)
                temp++;
        if(te==1){
            for(int i=0;i<temp;i++)
                b.clockwise(a, start,temp,i);
            for(int i=0;i<=amount-temp;i++)
                b.anticlockwise(a, start,temp,i);
        }else{
            for(int i=1;i<=amount-temp;i++)
                b.anticlockwise(a, start,temp,i);
            for(int i=0;i<temp;i++)
                b.clockwise(a, start,temp,i);
        }
        
    }

}

SSTF

package four;

import java.util.Arrays;
import java.util.Scanner;
class Array{
    int lo[];
    boolean []use;
    public int[] getA() {
        return lo;
    }
    public void setA(int[] a) {
        this.lo = a;
    }
    public boolean[] isUse() {
        return use;
    }
    public void setUse(boolean[] use) {
        this.use = use;
    }
    
}
public class SsTF {
    public int min(Array longth,int begin){
        int []a = new int[longth.lo.length];
        for(int i=0;i<longth.lo.length;i++)
            a[i]=Math.abs(longth.lo[i]-begin);
        int b[]=new int[longth.lo.length];
        for(int i=0;i<a.length;i++)
            b[i]=a[i];
//        for(int i=0;i<5;i++)
//            System.out.println("a-----"+a[i]);
        
        Arrays.sort(a);
//        for(int i=0;i<5;i++)
//            System.out.println("b-----"+b[i]);
        int c = 0;
        for(int i=0;i<longth.lo.length;i++)
            for(int j=0;j<longth.lo.length;j++)
                if(a[i]==b[j]&&longth.use[j]==false){
                    System.out.println(longth.lo[j]);
                    c=longth.lo[j];
                    longth.use[j]=true;
                    return longth.lo[j];
                }
                
       return c;
        
    }
    
    
    public static void main(String[] args) {
        int start;
        System.out.println("输入读写头");
        Scanner in=new Scanner(System.in);
        start=in.nextInt();
        System.out.println("输入序列长度");
        int amount;
        amount=in.nextInt();
        Array a=new Array();
        System.out.println("输入序列");
        int []lon=new int[amount];
        boolean []using=new boolean[amount];
        for(int i=0;i<amount;i++){
            lon[i]=in.nextInt();
            using[i]=false;
        }
        a.setA(lon);
        a.setUse(using);
        SsTF s=new SsTF();
        for(int i=0;i<amount;i++){
            int c=s.min(a, start);
            start=c;
        }
        
        
        
        
    }
}

 

posted @ 2016-07-10 14:17  鸣灭  阅读(3555)  评论(0编辑  收藏  举报