Related Articles
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
• Difficulty Level : Easy
• Last Updated : 20 Mar, 2020

Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.

• Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.
• It is a Greedy Algorithm.
• It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of ageing.
• It is practically infeasible as Operating System may not know burst time and therefore may not sort them. While it is not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a weighted average of previous execution times. SJF can be used in specialized environments where accurate estimates of running time are available.

Algorithm:

1. Sort all the process according to the arrival time.
2. Then select that process which has minimum arrival time and minimum Burst time.
3. After completion of process make a pool of process which after till the completion of previous process and select that process among the pool which is having minimum Burst time. How to compute below times in SJF using a program?

1. Completion Time: Time at which process completes its execution.
2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

In this post, we have assumed arrival times as 0, so turn around and completion times are same.

## C++

 `// C++ program to implement Shortest Job first with Arrival Time``#include``using` `namespace` `std;``int` `mat;`` ` `void` `swap(``int` `*a, ``int` `*b)``{``    ``int` `temp = *a;``    ``*a = *b;``    ``*b = temp;``}`` ` `void` `arrangeArrival(``int` `num, ``int` `mat[])``{``    ``for``(``int` `i=0; i mat[j+1])``            ``{``                ``for``(``int` `k=0; k<5; k++)``                ``{``                    ``swap(mat[j][k], mat[j+1][k]);``                ``}``            ``}``        ``}``    ``}``}`` ` `void` `completionTime(``int` `num, ``int` `mat[])``{``    ``int` `temp, val;``    ``mat = mat + mat;``    ``mat = mat - mat;``    ``mat = mat - mat;``     ` `    ``for``(``int` `i=1; i= mat[j] && low >= mat[j])``            ``{``                ``low = mat[j];``                ``val = j;``            ``}``        ``}``        ``mat[val] = temp + mat[val];``        ``mat[val] = mat[val] - mat[val];``        ``mat[val] = mat[val] - mat[val];``        ``for``(``int` `k=0; k<6; k++)``        ``{``            ``swap(mat[val][k], mat[i][k]);``        ``}``    ``}``}`` ` `int` `main()``{``    ``int` `num, temp;``     ` `    ``cout<<``"Enter number of Process: "``;``    ``cin>>num;``     ` `    ``cout<<``"...Enter the process ID...\n"``;``    ``for``(``int` `i=0; i>mat[i];``        ``cout<<``"Enter Arrival Time: "``;``        ``cin>>mat[i];``        ``cout<<``"Enter Burst Time: "``;``        ``cin>>mat[i];``    ``}``     ` `    ``cout<<``"Before Arrange...\n"``;``    ``cout<<``"Process ID\tArrival Time\tBurst Time\n"``;``    ``for``(``int` `i=0; i

## Java

 `// Java program to implement Shortest Job first with Arrival Time``import` `java.util.*;`` ` `class` `GFG {`` ` `    ``static` `int``[][] mat = ``new` `int``[``10``][``6``];`` ` `    ``static` `void` `arrangeArrival(``int` `num, ``int``[][] mat) {``        ``for` `(``int` `i = ``0``; i < num; i++) {``            ``for` `(``int` `j = ``0``; j < num - i - ``1``; j++) {``                ``if` `(mat[j][``1``] > mat[j + ``1``][``1``]) {``                    ``for` `(``int` `k = ``0``; k < ``5``; k++) {``                        ``int` `temp = mat[j][k];``                        ``mat[j][k] = mat[j + ``1``][k];``                        ``mat[j + ``1``][k] = temp;``                    ``}``                ``}``            ``}``        ``}``    ``}`` ` `    ``static` `void` `completionTime(``int` `num, ``int``[][] mat) {``        ``int` `temp, val = -``1``;``        ``mat[``0``][``3``] = mat[``0``][``1``] + mat[``0``][``2``];``        ``mat[``0``][``5``] = mat[``0``][``3``] - mat[``0``][``1``];``        ``mat[``0``][``4``] = mat[``0``][``5``] - mat[``0``][``2``];`` ` `        ``for` `(``int` `i = ``1``; i < num; i++) {``            ``temp = mat[i - ``1``][``3``];``            ``int` `low = mat[i][``2``];``            ``for` `(``int` `j = i; j < num; j++) {``                ``if` `(temp >= mat[j][``1``] && low >= mat[j][``2``]) {``                    ``low = mat[j][``2``];``                    ``val = j;``                ``}``            ``}``            ``mat[val][``3``] = temp + mat[val][``2``];``            ``mat[val][``5``] = mat[val][``3``] - mat[val][``1``];``            ``mat[val][``4``] = mat[val][``5``] - mat[val][``2``];``            ``for` `(``int` `k = ``0``; k < ``6``; k++) {``                ``int` `tem = mat[val][k];``                ``mat[val][k] = mat[i][k];``                ``mat[i][k] = tem;``            ``}``        ``}``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args) {``        ``int` `num;`` ` `        ``Scanner sc = ``new` `Scanner(System.in);`` ` `        ``System.out.println(``"Enter number of Process: "``);``        ``num = sc.nextInt();`` ` `        ``System.out.println(``"...Enter the process ID..."``);``        ``for` `(``int` `i = ``0``; i < num; i++) {``            ``System.out.println(``"...Process "` `+ (i + ``1``) + ``"..."``);``            ``System.out.println(``"Enter Process Id: "``);``            ``mat[i][``0``] = sc.nextInt();``            ``System.out.println(``"Enter Arrival Time: "``);``            ``mat[i][``1``] = sc.nextInt();``            ``System.out.println(``"Enter Burst Time: "``);``            ``mat[i][``2``] = sc.nextInt();``        ``}`` ` `        ``System.out.println(``"Before Arrange..."``);``        ``System.out.println(``"Process ID\tArrival Time\tBurst Time"``);``        ``for` `(``int` `i = ``0``; i < num; i++) {``            ``System.out.printf(``"%d\t\t%d\t\t%d\n"``, ``                ``mat[i][``0``], mat[i][``1``], mat[i][``2``]);``        ``}`` ` `        ``arrangeArrival(num, mat);``        ``completionTime(num, mat);``        ``System.out.println(``"Final Result..."``);``        ``System.out.println(``"Process ID\tArrival Time\tBurst"` `+``                        ``" Time\tWaiting Time\tTurnaround Time"``);``        ``for` `(``int` `i = ``0``; i < num; i++) {``            ``System.out.printf(``"%d\t\t%d\t\t%d\t\t%d\t\t%d\n"``, ``             ``mat[i][``0``], mat[i][``1``], mat[i][``2``], mat[i][``4``], mat[i][``5``]);``        ``}``        ``sc.close();``    ``}``}`` ` `// This code is contributed by``// sanjeev2552`

Output:

```Process ID      Arrival Time    Burst Time
1               2               3
2               0               4
3               4               2
4               5               4
Final Result...
Process ID      Arrival Time    Burst Time      Waiting Time    Turnaround Time
2               0               4               0               4
3               4               2               0               2
1               2               3               4               7
4               5               4               4               8
```

In Set-2 we will discuss the preemptive version of SJF i.e. Shortest Remaining Time First

This article is contributed by Mahesh Kumar(NCE, Chandi). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.