-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathScanScheduling.java
82 lines (71 loc) · 2.79 KB
/
ScanScheduling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.thealgorithms.scheduling.diskscheduling;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* https://en.wikipedia.org/wiki/Elevator_algorithm
* SCAN Scheduling algorithm implementation.
* The SCAN algorithm moves the disk arm towards one end of the disk, servicing all requests
* along the way until it reaches the end. Once it reaches the end, it reverses direction
* and services the requests on its way back.
*
* This algorithm ensures that all requests are serviced in a fair manner,
* while minimizing the seek time for requests located close to the current position
* of the disk arm.
*
* The SCAN algorithm is particularly useful in environments with a large number of
* disk requests, as it reduces the overall movement of the disk arm compared to
*/
public class ScanScheduling {
private int headPosition;
private int diskSize;
private boolean movingUp;
public ScanScheduling(int headPosition, boolean movingUp, int diskSize) {
this.headPosition = headPosition;
this.movingUp = movingUp;
this.diskSize = diskSize;
}
public List<Integer> execute(List<Integer> requests) {
// If the request list is empty, return an empty result
if (requests.isEmpty()) {
return new ArrayList<>();
}
List<Integer> result = new ArrayList<>();
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
// Separate requests into those smaller than the current head position and those larger
for (int request : requests) {
if (request < headPosition) {
left.add(request);
} else {
right.add(request);
}
}
// Sort the requests
Collections.sort(left);
Collections.sort(right);
// Simulate the disk head movement
if (movingUp) {
// Head moving upward, process right-side requests first
result.addAll(right);
// After reaching the end of the disk, reverse direction and process left-side requests
result.add(diskSize - 1); // Simulate the head reaching the end of the disk
Collections.reverse(left);
result.addAll(left);
} else {
// Head moving downward, process left-side requests first
Collections.reverse(left);
result.addAll(left);
// After reaching the start of the disk, reverse direction and process right-side requests
result.add(0); // Simulate the head reaching the start of the disk
result.addAll(right);
}
return result;
}
public int getHeadPosition() {
return headPosition;
}
public boolean isMovingUp() {
return movingUp;
}
}