2
2
3
3
import com .thealgorithms .devutils .entities .ProcessDetails ;
4
4
import java .util .ArrayList ;
5
+ import java .util .Collection ;
6
+ import java .util .Comparator ;
7
+ import java .util .Iterator ;
5
8
import java .util .List ;
6
9
7
10
/**
8
- * Implementation of Shortest Job First Algorithm: The algorithm allows the waiting process with the
9
- * minimal burst time to be executed first. see more here:
10
- * https://www.guru99.com/shortest-job-first-sjf-scheduling.html
11
+ * Shortest Job First (SJF) Scheduling Algorithm:
12
+ * Executes processes with the shortest burst time first among the ones that have arrived.
11
13
*/
12
-
13
14
public class SJFScheduling {
14
- protected ArrayList <ProcessDetails > processes ;
15
- protected ArrayList <String > schedule ;
16
-
17
- private static void sortProcessesByArrivalTime (List <ProcessDetails > processes ) {
18
- for (int i = 0 ; i < processes .size (); i ++) {
19
- for (int j = i + 1 ; j < processes .size () - 1 ; j ++) {
20
- if (processes .get (j ).getArrivalTime () > processes .get (j + 1 ).getArrivalTime ()) {
21
- final var temp = processes .get (j );
22
- processes .set (j , processes .get (j + 1 ));
23
- processes .set (j + 1 , temp );
24
- }
25
- }
26
- }
27
- }
15
+ private final List <ProcessDetails > processes ;
16
+ private final List <String > schedule ;
28
17
29
- /**
30
- * a simple constructor
31
- * @param processes a list of processes the user wants to schedule
32
- * it also sorts the processes based on the time of their arrival
33
- */
34
- SJFScheduling (final ArrayList <ProcessDetails > processes ) {
35
- this .processes = processes ;
36
- schedule = new ArrayList <>();
18
+ public SJFScheduling (final List <ProcessDetails > processes ) {
19
+ this .processes = new ArrayList <>(processes );
20
+ this .schedule = new ArrayList <>();
37
21
sortProcessesByArrivalTime (this .processes );
38
22
}
39
- protected void sortByArrivalTime () {
40
- sortProcessesByArrivalTime (processes );
23
+
24
+ private static void sortProcessesByArrivalTime (List <ProcessDetails > processes ) {
25
+ processes .sort (Comparator .comparingInt (ProcessDetails ::getArrivalTime ));
41
26
}
42
27
43
28
/**
44
- * this functions returns the order of the executions
29
+ * Executes the SJF scheduling algorithm and builds the execution order.
45
30
*/
46
-
47
31
public void scheduleProcesses () {
48
- ArrayList <ProcessDetails > ready = new ArrayList <>();
49
-
32
+ List <ProcessDetails > ready = new ArrayList <>();
50
33
int size = processes .size ();
51
- int runtime ;
52
34
int time = 0 ;
53
35
int executed = 0 ;
54
- int j ;
55
- int k = 0 ;
56
- ProcessDetails running ;
57
36
58
- if (size == 0 ) {
59
- return ;
37
+ Iterator <ProcessDetails > processIterator = processes .iterator ();
38
+
39
+ // This will track the next process to be checked for arrival time
40
+ ProcessDetails nextProcess = null ;
41
+ if (processIterator .hasNext ()) {
42
+ nextProcess = processIterator .next ();
60
43
}
61
44
62
45
while (executed < size ) {
63
- while (k < size && processes .get (k ).getArrivalTime () <= time ) // here we find the processes that have arrived.
64
- {
65
- ready .add (processes .get (k ));
66
- k ++;
46
+ // Load all processes that have arrived by current time
47
+ while (nextProcess != null && nextProcess .getArrivalTime () <= time ) {
48
+ ready .add (nextProcess );
49
+ if (processIterator .hasNext ()) {
50
+ nextProcess = processIterator .next ();
51
+ } else {
52
+ nextProcess = null ;
53
+ }
67
54
}
68
55
69
- running = findShortestJob (ready );
56
+ ProcessDetails running = findShortestJob (ready );
70
57
if (running == null ) {
71
58
time ++;
72
59
} else {
73
- runtime = running .getBurstTime ();
74
- for (j = 0 ; j < runtime ; j ++) {
75
- time ++;
76
- }
60
+ time += running .getBurstTime ();
77
61
schedule .add (running .getProcessId ());
78
62
ready .remove (running );
79
63
executed ++;
@@ -82,30 +66,23 @@ public void scheduleProcesses() {
82
66
}
83
67
84
68
/**
85
- * this function evaluates the shortest job of all the ready processes (based on a process
86
- * burst time)
69
+ * Finds the process with the shortest job of all the ready processes (based on a process
87
70
* @param readyProcesses an array list of ready processes
88
71
* @return returns the process' with the shortest burst time OR NULL if there are no ready
89
72
* processes
90
73
*/
91
- private ProcessDetails findShortestJob (List <ProcessDetails > readyProcesses ) {
92
- if (readyProcesses .isEmpty ()) {
93
- return null ;
94
- }
95
- int i ;
96
- int size = readyProcesses .size ();
97
- int minBurstTime = readyProcesses .get (0 ).getBurstTime ();
98
- int temp ;
99
- int positionOfShortestJob = 0 ;
74
+ private ProcessDetails findShortestJob (Collection <ProcessDetails > readyProcesses ) {
75
+ return readyProcesses .stream ().min (Comparator .comparingInt (ProcessDetails ::getBurstTime )).orElse (null );
76
+ }
100
77
101
- for (i = 1 ; i < size ; i ++) {
102
- temp = readyProcesses .get (i ).getBurstTime ();
103
- if (minBurstTime > temp ) {
104
- minBurstTime = temp ;
105
- positionOfShortestJob = i ;
106
- }
107
- }
78
+ /**
79
+ * Returns the computed schedule after calling scheduleProcesses().
80
+ */
81
+ public List <String > getSchedule () {
82
+ return schedule ;
83
+ }
108
84
109
- return readyProcesses .get (positionOfShortestJob );
85
+ public List <ProcessDetails > getProcesses () {
86
+ return List .copyOf (processes );
110
87
}
111
88
}
0 commit comments