22
33import com .thealgorithms .devutils .entities .ProcessDetails ;
44import java .util .ArrayList ;
5+ import java .util .Collection ;
6+ import java .util .Comparator ;
7+ import java .util .Iterator ;
58import java .util .List ;
69
710/**
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.
1113 */
12-
1314public 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 ;
2817
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 <>();
3721 sortProcessesByArrivalTime (this .processes );
3822 }
39- protected void sortByArrivalTime () {
40- sortProcessesByArrivalTime (processes );
23+
24+ private static void sortProcessesByArrivalTime (List <ProcessDetails > processes ) {
25+ processes .sort (Comparator .comparingInt (ProcessDetails ::getArrivalTime ));
4126 }
4227
4328 /**
44- * this functions returns the order of the executions
29+ * Executes the SJF scheduling algorithm and builds the execution order.
4530 */
46-
4731 public void scheduleProcesses () {
48- ArrayList <ProcessDetails > ready = new ArrayList <>();
49-
32+ List <ProcessDetails > ready = new ArrayList <>();
5033 int size = processes .size ();
51- int runtime ;
5234 int time = 0 ;
5335 int executed = 0 ;
54- int j ;
55- int k = 0 ;
56- ProcessDetails running ;
5736
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 ();
6043 }
6144
6245 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+ }
6754 }
6855
69- running = findShortestJob (ready );
56+ ProcessDetails running = findShortestJob (ready );
7057 if (running == null ) {
7158 time ++;
7259 } else {
73- runtime = running .getBurstTime ();
74- for (j = 0 ; j < runtime ; j ++) {
75- time ++;
76- }
60+ time += running .getBurstTime ();
7761 schedule .add (running .getProcessId ());
7862 ready .remove (running );
7963 executed ++;
@@ -82,30 +66,23 @@ public void scheduleProcesses() {
8266 }
8367
8468 /**
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
8770 * @param readyProcesses an array list of ready processes
8871 * @return returns the process' with the shortest burst time OR NULL if there are no ready
8972 * processes
9073 */
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+ }
10077
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+ }
10884
109- return readyProcesses .get (positionOfShortestJob );
85+ public List <ProcessDetails > getProcesses () {
86+ return List .copyOf (processes );
11087 }
11188}
0 commit comments