SJF (Shortest Job First) in Operating Systems - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Analyzing time complexity helps us understand how the scheduling process scales as more jobs arrive.
We want to know how the time to pick the next shortest job grows with the number of jobs waiting.
Analyze the time complexity of the following SJF scheduling snippet.
// jobs is a list of processes with their burst times
while (jobs not empty) {
shortest = find job with smallest burst time in jobs
run shortest job
remove shortest job from jobs
}
This code repeatedly selects and runs the job with the shortest burst time until all jobs are done.
Look for repeated actions that take time as jobs increase.
- Primary operation: Searching the list of jobs to find the shortest burst time.
- How many times: Once for each job, since each job is run and removed one by one.
Each time we pick a job, we scan all remaining jobs to find the shortest one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 + 9 + ... + 1 = 55 scans |
| 100 | About 100 + 99 + ... + 1 = 5,050 scans |
| 1000 | About 1000 + 999 + ... + 1 = 500,500 scans |
Pattern observation: The total work grows roughly like the square of the number of jobs.
Time Complexity: O(n²)
This means the time to schedule all jobs grows quickly as the number of jobs increases, roughly proportional to the square of the job count.
[X] Wrong: "Finding the shortest job each time only takes constant time because we just pick one job."
[OK] Correct: Actually, without special data structures, you must check all remaining jobs each time, so the time grows with the number of jobs left.
Understanding how scheduling algorithms scale helps you explain trade-offs clearly and shows you can think about efficiency beyond just correctness.
"What if we kept the jobs sorted by burst time from the start? How would that change the time complexity?"
Practice
SJF (Shortest Job First) scheduling algorithm?Solution
Step 1: Understand SJF scheduling principle
SJF always picks the job with the shortest execution time next to run.Step 2: Identify the goal of SJF
This approach reduces the average waiting time for all jobs in the queue.Final Answer:
To schedule the shortest job next to minimize average waiting time -> Option AQuick Check:
SJF = shortest job first, reduces waiting time [OK]
- Confusing SJF with FCFS (First Come First Serve)
- Thinking SJF schedules longest jobs first
- Assuming SJF schedules jobs randomly
Solution
Step 1: Recall SJF scheduling criteria
SJF selects the job with the shortest burst (execution) time next.Step 2: Match the description to options
Only Schedules the job with the shortest burst time next correctly states scheduling by shortest burst time.Final Answer:
Schedules the job with the shortest burst time next -> Option BQuick Check:
SJF = shortest burst time scheduling [OK]
- Confusing SJF with FCFS which uses arrival time
- Mixing SJF with round-robin scheduling
- Ignoring job length in scheduling decision
Job A: 6 units, Job B: 2 units, Job C: 8 units, Job D: 3 unitsWhat is the average waiting time using non-preemptive SJF scheduling?
Solution
Step 1: Order jobs by burst time for SJF
Order: Job B (2), Job D (3), Job A (6), Job C (8).Step 2: Calculate waiting times for each job
Waiting times: B=0, D=2, A=5 (2+3), C=11 (2+3+6).Step 3: Compute average waiting time
Average = (0 + 2 + 5 + 11) / 4 = 18 / 4 = 4.5 units.Final Answer:
4.5 units -> Option CQuick Check:
Average waiting time = 4.5 units [OK]
- Not sorting jobs by burst time
- Calculating waiting time incorrectly by mixing completion times
- Forgetting to start first job waiting time at zero
Job X: 4 units, Job Y: 3 units, Job Z: 5 unitsIf the scheduler mistakenly picks Job Z first, what is the main error?
Solution
Step 1: Identify correct SJF behavior
SJF should pick the job with the shortest burst time first, which is Job Y (3 units).Step 2: Analyze the mistake
Picking Job Z (5 units) first ignores the shortest job first rule.Final Answer:
Ignoring the shortest job first rule -> Option DQuick Check:
Picking longer job first breaks SJF rule [OK]
- Confusing arrival time with burst time priority
- Mixing preemptive and non-preemptive concepts
- Assuming random scheduling is allowed in SJF
Solution
Step 1: Understand preemptive SJF behavior
Preemptive SJF (Shortest Remaining Time First) allows interruption if a shorter job arrives.Step 2: Apply rule to scenario
If new job's burst time is less than current job's remaining time, it preempts immediately.Final Answer:
The new job preempts the current job immediately -> Option AQuick Check:
Preemptive SJF switches to shortest remaining job [OK]
- Assuming current job always runs to completion
- Confusing preemptive with non-preemptive SJF
- Thinking jobs run in parallel
