使用比较最短寻道时间优先算法 [英] Shortest seek time first algorithm using Comparator

查看:816
本文介绍了使用比较最短寻道时间优先算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作使用营运基金算法的java.util.Comparator
这是我到目前为止有:

Working on a SSTF algorithm using java.util.Comparator
This is what i have so far:

private int nextHeadPosition;

public SSTF(int currentHeadPosition) {
    nextHeadPosition = currentHeadPosition;
}

@Override
public int compare(DiskRequest r1, DiskRequest r2) {         
    if (nextHeadPosition - r1.getTrackNumber()  < nextHeadPosition -  r2.getTrackNumber()) {
        nextHeadPosition = r1.getTrackNumber();
        return -1;
    } else if (nextHeadPosition - r1.getTrackNumber() > nextHeadPosition - r2.getTrackNumber()) {
         nextHeadPosition = r2.getTrackNumber();
        return 1;
    } else {
        return 0;
    }
}

以50它是生产该订单的初始头部位置:

with an initial head position of 50 it is producing this order:

[100, 99, 50, 45, 44, 1]

我想输出产生:

The output I am trying to produce:

[50, 45, 44, 1, 99, 100]

这可能不是可配合比较

this might not be posible with comparator


编辑

有具有磁道号,第一个请求要被服务将是最接近于头的当前位置的轨道的请求队列。每个后续的请求将被至少距离最后一个请求的位置进行排序。

for a queue of requests that have track numbers, the first request to be serviced will be the track that is closest to the current position of the head. Each subsequent request will be ordered by least distance from the position of the last request.

所以对于与轨道队列 [100,99,50,45,44,1] 和50当前磁头位置,第一个请求将是50。接下来将轨道最接近50,这是在这种情况下,45。泡沫冲洗干净再说。

so for a queue with tracks [100, 99, 50, 45, 44, 1] and a current head position of 50, the first request will be 50. The next will be the track closest to 50, which is 45 in this case. lather rinse repeat.

推荐答案

首先,你要比较的的距离的轨道和头部位置之间,所以你必须使用绝对值在你的条件。

First, you want to compare the distance between the track and the head position, so you have to use the absolute value in your conditions.

if (Math.abs(nextHeadPosition - r1.getTrackNumber())  < Math.abs(nextHeadPosition -  r2.getTrackNumber()))

但你的方法修改的对象,这是不是一个好主意,因为你不知道如何 Col​​lections.sort()(我想这是你想要使用的)将使用它。你必须编写自己的排序算法,我认为。

But your compare method modifies the object, which is not a good idea since you don't know how Collections.sort() (I guess that's what you're trying to use) will use it. You have to write your own sorting algorithm I think.

这篇关于使用比较最短寻道时间优先算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆