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