Shortest Job First
Shortest Job First is a GFG problem (Medium). You are given an array bt of burst times: bt[i] is how long process i needs the CPU. Every process is ready at time 0, and the CPU runs one process at a time, start to finish.
Schedule them by shortest job first (the shortest burst runs next) and return the average waiting time, rounded down to an integer.
- A process's waiting time is how long it sits ready but not running. Here that is simply its start time.
nand each burst go up to 10⁵, so the total wait can reach about 5 × 10¹⁴.
- 1 <= n <= 10⁵
- 1 <= bt[i] <= 10⁵
- Every process is available at time 0
Intuition
Every job that runs adds its whole burst to the wait of every job behind it. So the order decides the total: a long job placed early is paid for by many jobs, while a long job placed last is paid for by none.
Running the jobs shortest first makes the delays that get counted most often the smallest ones. That is why SJF gives the lowest possible average waiting time. It is also non-preemptive: once a job starts, it runs to the end.
Because every job is ready at time 0, a job's wait is just the sum of the bursts that ran before it.
Whenever jobs must be ordered to minimise the total or average of their waiting or finishing times, sort by duration: it is the same swap argument each time. It appears as SJF in operating-system questions, as "minimise total waiting time", and in scheduling with a single machine. If jobs arrive at different times, the order is no longer a plain sort (see the FAQ).
Approach
Before reading on: for bursts [4, 1], work out the total waiting time if 4 runs first, and then if 1 runs first. What does the difference tell you about where long jobs belong?
Sort the burst times
Sort bt in increasing order. That is the whole schedule: index 0 runs first, then index 1, and so on. No job has to wait for an arrival, because all of them are ready at time 0.
Add up the start times
Keep clock, the time the CPU becomes free, and total, the waiting time so far. For each burst t in sorted order:
- add
clocktototal, because this job waited from 0 until now; - then add
ttoclock, because the job runs fort.
The order of these two lines matters: a job does not wait for itself.
Return the average, rounded down
Return total // n. GFG asks for the nearest integer not above the average, which is integer division. In C++ and Java, keep clock and total in a 64-bit type, since the total can pass 2³¹.
Why shortest first is the best order
Take any order where a longer job L runs just before a shorter job S. Swapping them leaves every other job's wait the same, adds S to L's wait and removes L from S's wait, so the total drops by L − S. Repeated swaps reach the sorted order, so nothing beats it.
Code in Python, C++ and Java
Common pitfalls
Counting a job's own run as waiting
clock += t total += clock
total += clock clock += t
Waiting time is the time before a job starts. Moving the clock first adds each job's own burst to its wait, which is the completion time, not the waiting time.
Forgetting to sort
for t in bt: # input order
bt.sort() for t in bt:
Running jobs in the given order is first-come, first-served. Its average is larger whenever a long job comes before a short one, and the sort is the entire SJF policy.
Keeping the total in a 32-bit int
int clock = 0, total = 0;
long long clock = 0, total = 0;
With 10⁵ jobs of length 10⁵, the clock reaches 10¹⁰ and the total about 5 × 10¹⁴. Both overflow an int in C++ and Java; Python integers do not overflow.
Rounding the average to the nearest integer
return round(total / len(bt))
return total // len(bt)
GFG wants the largest integer not above the average, so 4.6 must give 4. round gives 5, and floating-point division can also lose precision on totals this large.
Edge cases
It starts at time 0, so its wait is 0 and the average is 0. The loop handles it with no special case.
Complexity
This problem vs SJF with arrival times
A typical SJF program in OS courses, including GFG's article, handles processes that arrive at different times. That is a different, harder problem.
| This problem (all ready at 0) | Non-preemptive SJF with arrivals | Preemptive SJF (SRTF) | |
|---|---|---|---|
| Which job runs next | the next one in sorted order | the shortest among jobs that have arrived | the shortest remaining time, checked at every arrival |
| Tool | one sort | sort by arrival + min-heap by burst | min-heap, re-checked when a job arrives |
| CPU can sit idle | never | yes, until the next arrival | yes, until the next arrival |
| Waiting time | start time | start time − arrival | finish − arrival − burst |
Shortest Job First FAQ
What are the steps?
- Sort: sort the burst times in increasing order.
- Sum: walk the sorted list; add the current clock to the total wait, then add the burst to the clock.
- Answer: return
total // n. - Complexity: O(n log n) time, O(1) extra space.
What changes when processes arrive at different times?
Sort by arrival time and keep a min-heap of the jobs that have arrived, keyed by burst. Each time the CPU is free, push every job that has arrived by now, then pop the shortest and run it. If the heap is empty, jump the clock to the next arrival. Waiting time becomes start time − arrival time. It is still O(n log n).
How are turnaround time and waiting time related?
Turnaround time is completion time − arrival time, and waiting time is turnaround time − burst time. With every process arriving at 0, turnaround equals completion time and waiting equals start time, which is why this solution only needs the clock.
Can you walk through an example by hand?
Take bursts [4, 3, 7, 1, 2]. Sorted: [1, 2, 3, 4, 7]. The jobs start at 0, 1, 3, 6 and 10, so those are their waits. The total is 20, and 20 / 5 = 4. In the given order instead, the waits would be 0, 4, 7, 14 and 15, an average of 8.