磁盘最短寻道java怎么编写,使用比较最短寻道时间优先算法

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;

}

}

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

edit

SSTF

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.

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()))

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值