Skip to content

Commit b080a36

Browse files
committed
Add Disk Scheduling Algorithm
1 parent 0d3fadf commit b080a36

File tree

10 files changed

+689
-0
lines changed

10 files changed

+689
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.thealgorithms.scheduling.diskscheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* Circular Look Scheduling (C-LOOK) is a disk scheduling algorithm similar to
9+
* the C-SCAN algorithm but with a key difference. In C-LOOK, the disk arm also
10+
* moves in one direction to service requests, but instead of going all the way
11+
* to the end of the disk, it only goes as far as the furthest request in the
12+
* current direction. After servicing the last request in the current direction,
13+
* the arm immediately jumps back to the closest request on the other side without
14+
* moving to the disk's extreme ends. This reduces the unnecessary movement of the
15+
* disk arm, resulting in better performance compared to C-SCAN, while still
16+
* maintaining fair wait times for requests.
17+
*/
18+
public class CircularLookScheduling {
19+
private int currentPosition;
20+
private boolean movingUp;
21+
private final int maxCylinder;
22+
23+
public CircularLookScheduling(int startPosition, boolean movingUp, int maxCylinder) {
24+
this.currentPosition = startPosition;
25+
this.movingUp = movingUp;
26+
this.maxCylinder = maxCylinder;
27+
}
28+
29+
public List<Integer> execute(List<Integer> requests) {
30+
List<Integer> result = new ArrayList<>();
31+
32+
// Filter and sort valid requests in both directions
33+
List<Integer> upRequests = new ArrayList<>();
34+
List<Integer> downRequests = new ArrayList<>();
35+
36+
for (int request : requests) {
37+
if (request >= 0 && request < maxCylinder) {
38+
if (request > currentPosition) {
39+
upRequests.add(request);
40+
} else if (request < currentPosition) {
41+
downRequests.add(request);
42+
}
43+
}
44+
}
45+
46+
Collections.sort(upRequests);
47+
Collections.sort(downRequests);
48+
49+
if (movingUp) {
50+
// Process all requests in the upward direction
51+
for (int request : upRequests) {
52+
result.add(request);
53+
}
54+
55+
// Jump to the lowest request and process all requests in the downward direction
56+
if (!downRequests.isEmpty()) {
57+
result.addAll(downRequests);
58+
}
59+
} else {
60+
// Process all requests in the downward direction (in reverse order)
61+
for (int i = downRequests.size() - 1; i >= 0; i--) {
62+
result.add(downRequests.get(i));
63+
}
64+
65+
// Jump to the highest request and process all requests in the upward direction (in reverse order)
66+
if (!upRequests.isEmpty()) {
67+
for (int i = upRequests.size() - 1; i >= 0; i--) {
68+
result.add(upRequests.get(i));
69+
}
70+
}
71+
72+
}
73+
74+
// Update current position to the last processed request
75+
if (!result.isEmpty()) {
76+
currentPosition = result.get(result.size() - 1);
77+
}
78+
79+
return result;
80+
}
81+
82+
public int getCurrentPosition() {
83+
return currentPosition;
84+
}
85+
86+
public boolean isMovingUp() {
87+
return movingUp;
88+
}
89+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.scheduling.diskscheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* Circular Scan Scheduling (C-SCAN) is a disk scheduling algorithm that
9+
* works by moving the disk arm in one direction to service requests until
10+
* it reaches the end of the disk. Once it reaches the end, instead of reversing
11+
* direction like in the SCAN algorithm, the arm moves back to the starting point
12+
* without servicing any requests. This ensures a more uniform wait time for all
13+
* requests, especially those near the disk edges. The algorithm then continues in
14+
* the same direction, making it effective for balancing service time across all disk sectors.
15+
*/
16+
public class CircularScanScheduling {
17+
private int currentPosition;
18+
private boolean movingUp;
19+
private final int diskSize;
20+
21+
public CircularScanScheduling(int startPosition, boolean movingUp, int diskSize) {
22+
this.currentPosition = startPosition;
23+
this.movingUp = movingUp;
24+
this.diskSize = diskSize;
25+
}
26+
27+
public List<Integer> execute(List<Integer> requests) {
28+
if (requests.isEmpty()) {
29+
return new ArrayList<>(); // Return empty list if there are no requests
30+
}
31+
32+
List<Integer> sortedRequests = new ArrayList<>(requests);
33+
Collections.sort(sortedRequests);
34+
35+
List<Integer> result = new ArrayList<>();
36+
37+
if (movingUp) {
38+
// Moving up: process requests >= current position
39+
for (int request : sortedRequests) {
40+
if (request >= currentPosition) {
41+
result.add(request);
42+
}
43+
}
44+
45+
// Jump to the smallest request and continue processing from the start
46+
for (int request : sortedRequests) {
47+
if (request < currentPosition) {
48+
result.add(request);
49+
}
50+
}
51+
} else {
52+
// Moving down: process requests <= current position in reverse order
53+
for (int i = sortedRequests.size() - 1; i >= 0; i--) {
54+
int request = sortedRequests.get(i);
55+
if (request <= currentPosition) {
56+
result.add(request);
57+
}
58+
}
59+
60+
// Jump to the largest request and continue processing in reverse order
61+
for (int i = sortedRequests.size() - 1; i >= 0; i--) {
62+
int request = sortedRequests.get(i);
63+
if (request > currentPosition) {
64+
result.add(request);
65+
}
66+
}
67+
}
68+
69+
// Set final position to the last request processed
70+
if (!result.isEmpty()) {
71+
currentPosition = result.get(result.size() - 1);
72+
}
73+
return result;
74+
}
75+
76+
public int getCurrentPosition() {
77+
return currentPosition;
78+
}
79+
80+
public boolean isMovingUp() {
81+
return movingUp;
82+
}
83+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.thealgorithms.scheduling.diskscheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* https://en.wikipedia.org/wiki/LOOK_algorithm
9+
* Look Scheduling algorithm implementation.
10+
* The Look algorithm moves the disk arm to the closest request in the current direction,
11+
* and once it processes all requests in that direction, it reverses the direction.
12+
*/
13+
public class LookScheduling {
14+
15+
private final int maxTrack;
16+
private final int currentPosition;
17+
private boolean movingUp;
18+
private int farthestPosition;
19+
20+
public LookScheduling(int startPosition, boolean initialDirection, int maxTrack) {
21+
this.currentPosition = startPosition;
22+
this.movingUp = initialDirection;
23+
this.maxTrack = maxTrack;
24+
}
25+
26+
/**
27+
* Executes the Look Scheduling algorithm on the given list of requests.
28+
*
29+
* @param requests List of disk requests.
30+
* @return Order in which requests are processed.
31+
*/
32+
public List<Integer> execute(List<Integer> requests) {
33+
List<Integer> result = new ArrayList<>();
34+
List<Integer> lower = new ArrayList<>();
35+
List<Integer> upper = new ArrayList<>();
36+
37+
// Split requests into two lists based on their position relative to current position
38+
for (int request : requests) {
39+
if (request < currentPosition) {
40+
lower.add(request);
41+
} else {
42+
upper.add(request);
43+
}
44+
}
45+
46+
// Sort the requests
47+
Collections.sort(lower);
48+
Collections.sort(upper);
49+
50+
// Process the requests depending on the initial moving direction
51+
if (movingUp) {
52+
// Process requests in the upward direction
53+
result.addAll(upper);
54+
if (!upper.isEmpty()) {
55+
farthestPosition = upper.get(upper.size() - 1);
56+
}
57+
58+
// Reverse the direction and process downward
59+
movingUp = false;
60+
Collections.reverse(lower);
61+
result.addAll(lower);
62+
if (!lower.isEmpty()) {
63+
farthestPosition = Math.max(farthestPosition, lower.get(0));
64+
}
65+
} else {
66+
// Process requests in the downward direction
67+
Collections.reverse(lower);
68+
result.addAll(lower);
69+
if (!lower.isEmpty()) {
70+
farthestPosition = lower.get(0);
71+
}
72+
73+
// Reverse the direction and process upward
74+
movingUp = true;
75+
result.addAll(upper);
76+
if (!upper.isEmpty()) {
77+
farthestPosition = Math.max(farthestPosition, upper.get(upper.size() - 1));
78+
}
79+
}
80+
81+
return result;
82+
}
83+
84+
public int getCurrentPosition() {
85+
return currentPosition;
86+
}
87+
88+
public boolean isMovingUp() {
89+
return movingUp;
90+
}
91+
92+
public int getFarthestPosition() {
93+
return farthestPosition;
94+
}
95+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.scheduling.diskscheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
*https://en.wikipedia.org/wiki/Shortest_seek_first
8+
* Shortest Seek First (SFF) Scheduling algorithm implementation.
9+
* The SFF algorithm selects the next request to be serviced based on the shortest distance
10+
* from the current position of the disk arm. It continuously evaluates all pending requests
11+
* and chooses the one that requires the least amount of movement to service.
12+
*
13+
* This approach minimizes the average seek time, making it efficient in terms of response
14+
* time for individual requests. However, it may lead to starvation for requests located
15+
* further away from the current position of the disk arm.
16+
*
17+
* The SFF algorithm is particularly effective in systems where quick response time
18+
* is crucial, as it ensures that the most accessible requests are prioritized for servicing.
19+
*/
20+
public class SSFScheduling {
21+
private int currentPosition;
22+
23+
public SSFScheduling(int currentPosition) {
24+
this.currentPosition = currentPosition;
25+
}
26+
27+
public List<Integer> execute(List<Integer> requests) {
28+
List<Integer> result = new ArrayList<>(requests);
29+
List<Integer> orderedRequests = new ArrayList<>();
30+
31+
while (!result.isEmpty()) {
32+
int closest = findClosest(result);
33+
orderedRequests.add(closest);
34+
result.remove(Integer.valueOf(closest));
35+
currentPosition = closest;
36+
}
37+
return orderedRequests;
38+
}
39+
40+
private int findClosest(List<Integer> requests) {
41+
int minDistance = Integer.MAX_VALUE;
42+
int closest = -1;
43+
for (int request : requests) {
44+
int distance = Math.abs(currentPosition - request);
45+
if (distance < minDistance) {
46+
minDistance = distance;
47+
closest = request;
48+
}
49+
}
50+
return closest;
51+
}
52+
53+
public int getCurrentPosition() {
54+
return currentPosition;
55+
}
56+
}

0 commit comments

Comments
 (0)