public static double cscan(int[] requests, int head) { int n = requests.length; Arrays.sort(requests); int index = 0; for (int i = 0; i < n; i++) { if (requests[i] > head) { index = i; break; } } int distance = 0; if (index != 0) { for (int i = index - 1; i >= 0; i--) { distance += head - requests[i]; head = requests[i]; } } distance += head; head = n - 1; for (int i = n - 1; i >= index; i--) { distance += head - requests[i]; head = requests[i]; } distance += head; return (double) distance / n; } 解释一下上述代码的意识
时间: 2024-02-14 07:05:51 浏览: 20
这段代码是实现 C-Scan 硬盘调度算法的函数。该算法是一种循环扫描调度算法,在扫描过程中只考虑请求序列中比当前磁头位置大的请求,扫描到序列尾后重新从序列头开始扫描,直到再次扫描到磁头位置。
代码中首先对请求序列进行排序,然后找到第一个比磁头位置大的请求的下标 index。接着,从 index 前一个请求开始向磁头位置的前面扫描,计算出磁头移动的距离 distance。如果有比磁头位置小的请求,则需要将磁头移动到序列的最后一个请求,然后再从 index 开始向磁头位置的后面扫描,同样计算出磁头移动的距离 distance。最后将总的磁头移动距离 distance 除以请求的总数 n,得到平均寻道长度。
相关问题
// 循环SCAN算法 public static double cscan(int[] requests, int head) { int n = requests.length; Arrays.sort(requests); int index = 0; for (int i = 0; i < n; i++) { if (requests[i] > head) { index = i; break; } } int distance = 0; if (index != 0) { for (int i
循环SCAN算法(Circular SCAN,C-SCAN)是一种改进的SCAN算法,它在磁盘的两端分别设置哨兵点,当磁头到达一个端点时,它会立即移动到另一个端点,从而避免了电梯效应。
以下是代码的解释:
首先对请求进行排序,然后找到第一个大于磁头位置的请求,并记录其索引为index。
如果index不为0,则需要先访问小于磁头位置的请求,这部分请求的距离为head-requests[i]。
然后将磁头移动到0号磁道,再访问大于磁头位置的请求,这部分请求的距离为requests[i]-0。
接着将磁头移动到n-1号磁道,再访问哨兵点后的第一个请求,这部分请求的距离为n-1-requests[i]。
最后将磁头移动到第一个请求,再访问哨兵点前面的请求,这部分请求的距离为requests[i]。
最后,将distance除以请求总数n,即可得到平均寻道长度。
以下是代码的实现:
```
public static double cscan(int[] requests, int head) {
int n = requests.length;
Arrays.sort(requests);
int index = 0;
for (int i = 0; i < n; i++) {
if (requests[i] > head) {
index = i;
break;
}
}
int distance = 0;
if (index != 0) {
for (int i = index - 1; i >= 0; i--) {
distance += head - requests[i];
head = requests[i];
}
distance += head - 0;
head = 0;
for (int i = index; i < n; i++) {
distance += requests[i] - head;
head = requests[i];
}
distance += n - 1 - head;
head = n - 1;
for (int i = 0; i < index; i++) {
distance += requests[i] - head;
head = requests[i];
}
} else {
for (int i = n - 1; i >= 0; i--) {
distance += head - requests[i];
head = requests[i];
}
distance += head - 0;
head = 0;
for (int i = 0; i < n; i++) {
distance += requests[i] - head;
head = requests[i];
}
}
return (double) distance / n;
}
```
写出FCFS SSTF SCAN CSCAN java代码
以下是FCFS、SSTF、SCAN、CSCAN算法的Java实现示例:
FCFS:
```java
public static int FCFS(int[] requests, int head) {
int total = 0;
int cur = head;
for (int i = 0; i < requests.length; i++) {
total += Math.abs(cur - requests[i]);
cur = requests[i];
}
return total;
}
```
SSTF:
```java
public static int SSTF(int[] requests, int head) {
int total = 0;
int cur = head;
boolean[] visited = new boolean[requests.length];
Arrays.fill(visited, false);
for (int i = 0; i < requests.length; i++) {
int minDist = Integer.MAX_VALUE;
int index = -1;
for (int j = 0; j < requests.length; j++) {
if (!visited[j]) {
int dist = Math.abs(cur - requests[j]);
if (dist < minDist) {
minDist = dist;
index = j;
}
}
}
visited[index] = true;
total += minDist;
cur = requests[index];
}
return total;
}
```
SCAN:
```java
public static int SCAN(int[] requests, int head, int max) {
int total = 0;
int cur = head;
boolean left = true;
Arrays.sort(requests);
int index = Arrays.binarySearch(requests, cur);
if (index < 0) {
index = -index - 1;
}
while (index < requests.length && requests[index] <= cur) {
index++;
}
if (index == requests.length) {
left = false;
index--;
}
while (true) {
if (left) {
total += cur;
if (index == 0) {
total += requests[index];
cur = requests[index];
left = false;
index++;
} else {
total += cur - requests[index - 1];
cur = requests[index - 1];
index--;
}
} else {
total += max - cur;
if (index == requests.length - 1) {
total += max - requests[index];
cur = requests[index];
left = true;
index--;
} else {
total += requests[index + 1] - cur;
cur = requests[index + 1];
index++;
}
}
if (index < 0 || index >= requests.length) {
break;
}
}
return total;
}
```
CSCAN:
```java
public static int CSCAN(int[] requests, int head, int max) {
int total = 0;
int cur = head;
boolean left = true;
Arrays.sort(requests);
int index = Arrays.binarySearch(requests, cur);
if (index < 0) {
index = -index - 1;
}
while (index < requests.length && requests[index] <= cur) {
index++;
}
if (index == requests.length) {
index = 0;
}
while (true) {
if (left) {
total += cur;
if (index == requests.length) {
total += max - cur;
cur = 0;
index = 0;
} else {
total += cur - requests[index - 1];
cur = requests[index - 1];
}
left = false;
} else {
total += max - cur;
if (index == 0) {
total += max - requests[requests.length - 1];
cur = max;
index = requests.length;
} else {
total += requests[index] - cur;
cur = requests[index];
}
left = true;
}
if (index == requests.length && !left) {
break;
}
}
return total;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)