A software engineer is tasked with using the shortest job first (SJF) policy to calculate the average waiting time for each process. The shortest job first also known as shortest job next (SJN) scheduling policy selects the waiting process with the least execution time to run next.
Given an array of n integers representing the burst times of processes, determine the average waiting time for all processes and return the closest whole number that is less than or equal to the result.
Input : bt = [4, 1, 3, 7, 2]
Output : 4
Explanation : The total waiting time is 20.
So the average waiting time will be 20/5 => 4.
Input : bt = [1, 2, 3, 4]
Output : 2
Explanation : The total waiting time is 10.
So the average waiting time will be 10/4 => 2.
Input : bt = [9, 3, 1, 8, 2]
Implementation of Shortest Job First Algorithm
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/*Function to calculate total waiting
time using Shortest Job First algorithm*/
long long solve(vector<int>& bt) {
// Sort jobs in ascending order
sort(bt.begin(), bt.end());
// Initialize total waiting time
long long waitTime = 0;
// Initialize total time taken
long long totalTime = 0;
// Get number of jobs
int n = bt.size();
// Iterate to calculate waiting time
for (int i = 0; i < n; ++i) {
waitTime += totalTime;
totalTime += bt[i];
}
// Return average waiting time
return waitTime/n;
}
};
int main() {
vector<int> jobs = {1, 2, 3, 4};
cout << "Array Representing Job Durations: ";
for (int i = 0; i < jobs.size(); i++) {
cout << jobs[i] << " ";
}
cout << endl;
Solution solution;
long long ans = solution.solve(jobs);
cout << "Total waiting time: " << ans << endl;
return 0;
}
import java.util.*;
class Solution {
/* Function to calculate total waiting
time using Shortest Job First algorithm */
public long solve(int[] bt) {
// Sort jobs in ascending order
Arrays.sort(bt);
// Initialize total waiting time
long waitTime = 0;
// Initialize total time taken
long totalTime = 0;
// Get number of jobs
int n = bt.length;
// Iterate to calculate waiting time
for (int i = 0; i < n; ++i) {
waitTime += totalTime;
totalTime += bt[i];
}
// Return average waiting time
return waitTime / n;
}
public static void main(String[] args) {
int[] jobs = {1, 2, 3, 4};
System.out.print("Array Representing Job Durations: ");
for (int job : jobs) {
System.out.print(job + " ");
}
System.out.println();
Solution solution = new Solution();
long ans = solution.solve(jobs);
System.out.println("Total waiting time: " + ans);
}
}
class Solution:
"""Function to calculate total waiting
time using Shortest Job First algorithm"""
def solve(self, bt):
# Sort jobs in ascending order
bt.sort()
# Initialize total waiting time
wait_time = 0
# Initialize total time taken
total_time = 0
# Get number of jobs
n = len(bt)
# Iterate to calculate waiting time
for i in range(n):
wait_time += total_time
total_time += bt[i]
# Return average waiting time
return wait_time // n
# Example usage
if __name__ == "__main__":
jobs = [1, 2, 3, 4]
print("Array Representing Job Durations: ", end="")
for job in jobs:
print(job, end=" ")
print()
solution = Solution()
ans = solution.solve(jobs)
print("Total waiting time: ", ans)
class Solution {
/* Function to calculate total waiting
time using Shortest Job First algorithm */
solve(bt) {
// Sort jobs in ascending order
bt.sort((a, b) => a - b);
// Initialize total waiting time
let waitTime = 0;
// Initialize total time taken
let totalTime = 0;
// Get number of jobs
let n = bt.length;
// Iterate to calculate waiting time
for (let i = 0; i < n; ++i) {
waitTime += totalTime;
totalTime += bt[i];
}
// Return average waiting time
return Math.floor(waitTime / n);
}
}
// Example usage
const jobs = [1, 2, 3, 4];
console.log("Array Representing Job Durations: " + jobs.join(" "));
const solution = new Solution();
const ans = solution.solve(jobs);
console.log("Total waiting time: " + ans);
Q: Why does sorting the burst times minimize the waiting time?
A: Sorting ensures shorter processes finish earlier, reducing the waiting time for longer processes that follow. This is the essence of the SJF policy.
Q: Does SJF consider arrival times of processes?
A: This question focuses on non-preemptive SJF, where all processes are assumed to arrive at time 0. For preemptive SJF (also known as Shortest Remaining Time First), arrival times are considered.
Q: How would you extend this to include arrival times?
A: Sort processes by arrival time first. For each time unit, select the process with the shortest burst time among those that have arrived but haven’t been executed.
Q: What are the limitations of SJF?
A: SJF may lead to starvation for processes with long burst times if shorter jobs keep arriving. It also assumes knowledge of burst times in advance, which may not always be possible.