接上一篇FCFS,SJF——Shortest-Job-First,最短作业优先算法
package org.cloudbus.cloudsim.examples;
public class SJF {
public static double SJF(int[] mas) {
double avg = 0;
System.out.println("");
System.out.println("Let's calculate the average waiting time using process_scheduling_algorithm.SJF algorithm");
int t;
for (int i = 0; i < mas.length; i++) {
for (int j = i + 1; j < mas.length; j++) {
if (mas[j] < mas[i]) {
t = mas[i];
mas[i] = mas[j];
mas[j] = t;
}
}
}
for (int i = 0; i < mas.length; i++) {
for (int k = i; k > 0; k--) {
avg += mas[k - 1];
}
}
double favg = avg / mas.length;
return favg;
}
}
结果展示:
同FCFS中所用到的数据,就是在计算之前现将任务按需要执行的时间由短到长进行排序:
9 37 42 45 62
输出结果:55.2((4×9+3×37+2×42+1×45)/5)