Recently I spent some time trying to implement the algorithm called Shortest Seek Time First
(SSTF) based on simulation, however I am running out of time and I cannot discover how to implement the algorithm well, since I have only half achieved it, the algorithm starts from something called cilindros
where a cylinder simulates to be the regions through which the needle of the hard disk is going to move, in short, where the needle of the hard disk is going to pass first.
The algorithm SSTF
works based on the shortest distance to travel from one cylinder to another, that is, the order is for the shortest distance to travel to get from one cylinder to another, I have done the following:
const timeList = new Array(4);
timeList[0] = {name: 'Open paint', time: 1000, cilinder: 83};
timeList[1] = {name: 'Open word', time: 2000, cilinder: 90};
timeList[2] = {name: 'Open photoshop', time: 4000, cilinder: 43};
timeList[3] = {name: 'Open gimpp', time: 3000, cilinder: 87};
function orderByLessCilinderDistance(processList){
let procList = new Array(...processList);
outer: for(let i = 0; i < procList.length - 1; i++){
for(let j = i + 1; j < procList.length; j++){
if(i > 0){
if(Math.abs(procList[i].cilinder - procList[j].cilinder) > Math.abs(procList[i - 1].cilinder - procList[i].cilinder)){
const swap = procList[i];
procList[i] = procList[j];
procList[j] = swap;
}
}else{
continue outer;
}
}
}
return procList
}
console.log(orderByLessCilinderDistance(timeList));
Where I have almost done it, that is, what the function returns orderByLessCilinderDistance
is:
[
{
"name": "Open paint",
"time": 1000,
"cilinder": 83
},
{
"name": "Open gimpp",
"time": 3000,
"cilinder": 87
},
{
"name": "Open photoshop",
"time": 4000,
"cilinder": 43
},
{
"name": "Open word",
"time": 2000,
"cilinder": 90
}
]
Which is correct, except the last two, because the order should be like this:
cilinder: 83,
cilinder: 87,
cilinder: 90,
cilinder: 43
Because according to this order from 83 to 87 there are only 4, from 87 to 90 there are only 3, from 90 to 43 is the longest distance, (the first two are correct because if instead of putting 87 first, put 90, then the distance would no longer be 4 in the first one, it would be 7, which is wrong for the algorithm).
What changes or fixes can I make to my code to get the result I want? I already tried a custom sort, but it still ordered it from smallest to largest or largest to smallest, but not in the order I want.
Keep in mind that the first element will always be the first, that is, the first is the starting point and will always be in the same place, the others must be ordered according to the minimum distance or shortest route between them and the first. whether the number is greater than the cylinder or less, what counts is ordering according to the distances between one cylinder and the other starting from the first element and that the first is always the first.
The reason it fails is because you're never really calculating how far the start point is from everyone else.
If you pay attention, you are always trading if the distance of
i-j > i-(i-1)
.Solution:
What you should do is first calculate the distance to everyone and then trade with the closest one .