Skip to content

Shortest Job First schedule algorithm implementation #3843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 0 additions & 199 deletions src/main/java/com/thealgorithms/others/SJF.java

This file was deleted.

115 changes: 115 additions & 0 deletions src/main/java/com/thealgorithms/scheduling/SJFScheduling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.thealgorithms.scheduling;

import com.thealgorithms.devutils.entities.ProcessDetails;

import java.util.ArrayList;

/**
* Implementation of Shortest Job First Algorithm: The algorithm allows the waiting process with the minimal burst time to be executed first.
* see more here: https://www.guru99.com/shortest-job-first-sjf-scheduling.html
*/

public class SJFScheduling {
protected ArrayList<ProcessDetails> processes;
protected ArrayList<String>schedule ;

/**
* a simple constructor
* @param processes a list of processes the user wants to schedule
* it also sorts the processes based on the time of their arrival
*/
SJFScheduling(final ArrayList<ProcessDetails> processes) {
this.processes = processes;
schedule=new ArrayList<>();
sortByArrivalTime();
}
protected void sortByArrivalTime() {
int size=processes.size(),i,j;
ProcessDetails temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size-1;j++)
{
if(processes.get(j).getArrivalTime()>processes.get(j+1).getArrivalTime())
{
temp=processes.get(j);
processes.set(j,processes.get(j+1));
processes.set(j+1,temp);
}
}
}

}

/**
* this functions returns the order of the executions
*/

public void scheduleProcesses() {
ArrayList<ProcessDetails> ready=new ArrayList<>();

int size = processes.size(),runtime,time=0;
int executed=0,j,k=0;
ProcessDetails running;

if (size == 0) {
return;
}


while(executed<size)
{
while(k<size && processes.get(k).getArrivalTime()<=time)//here we find the processes that have arrived.
{
ready.add(processes.get(k));
k++;
}

running=findShortestJob(ready);
if(running==null)
{
time++;
}
else {
runtime = running.getBurstTime();
for (j = 0; j < runtime; j++) {
time++;}
schedule.add(running.getProcessId());
ready.remove(running);
executed++;
}
}


}

/**
* this function evaluates the shortest job of all the ready processes (based on a process burst time)
* @param ReadyProcesses an array list of ready processes
* @return returns the process' with the shortest burst time OR NULL if there are no ready processes
*/
private ProcessDetails findShortestJob(ArrayList<ProcessDetails> ReadyProcesses) {
if (ReadyProcesses.isEmpty()){
return null;
}
int i,size = ReadyProcesses.size();
int minBurstTime = ReadyProcesses.get(0).getBurstTime(), temp, positionOfShortestJob = 0;


for (i = 1; i < size; i++) {
temp = ReadyProcesses.get(i).getBurstTime();
if (minBurstTime > temp ) {
minBurstTime = temp;
positionOfShortestJob = i;
}
}

return ReadyProcesses.get(positionOfShortestJob);
}





}

Loading