Skip to content

Commit 016c65b

Browse files
committed
Adds sample i/o and time and space complexity
1 parent e29c0e3 commit 016c65b

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## **Shortest Job First Scheduling Algorithm (SJF - Non-Preemptive) in C++**
2+
3+
Time Complexity : O(n^2)
4+
5+
Space Complexity : O(n)
6+
7+
Description : To calculate the average waiting time using the SJF(Non- Preemption)
8+
algorithm. First sort the processes in ascending order of their burst time and the process having
9+
the shortest burst time is executed first and so on. Second waiting time of the first process is kept
10+
zero and the waiting time of the second process is the burst time of the first process and the
11+
waiting time of the third process is the sum of the burst time of the first and the second process
12+
and so on. After calculating all the waiting times the average waiting time is calculated as the
13+
average of all the waiting times. SJF mainly says shortest job first the algorithm.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include<iostream>
2+
#include <stdlib.h>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int i,n,WaitT[20],BurstT[20],TurnaroundT[20],j,key;
9+
float AverageTT,AverageWT,TotalWT=0,TotalTT=0;
10+
cout << "Enter the number of processes : ";
11+
cin >> n;
12+
for(i=0;i<n;i++)
13+
{
14+
cout << "The burst time for process " << i << " = ";
15+
cin >> BurstT[i];
16+
}
17+
for (i=1;i<=n-1;i++)
18+
{
19+
j=i;
20+
while (j>0 && BurstT[j-1]>BurstT[j])
21+
{
22+
key = BurstT[j];
23+
BurstT[j] = BurstT[j-1];
24+
BurstT[j-1] = key;
25+
j=j-1;
26+
}
27+
}
28+
cout << "Sorted list in ascending order : " << endl;
29+
for (i=0;i<=n-1;i++)
30+
{
31+
cout << "Program with burst time " << BurstT[i] << endl;
32+
}
33+
WaitT[-1]=0;
34+
BurstT[-1]=0;
35+
WaitT[0]=0;
36+
TurnaroundT[0]=BurstT[0];
37+
for(i=0;i<n;i++)
38+
{
39+
WaitT[i]=WaitT[i-1]+BurstT[i-1];
40+
cout << "Waiting time for process " << i << " = " << WaitT[i] <<endl;
41+
TurnaroundT[i]=WaitT[i]+BurstT[i];
42+
cout << "Turnaround time for process " << i << " = " << TurnaroundT[i] << endl;
43+
}
44+
for(i=0;i<n;i++)
45+
{
46+
TotalWT = TotalWT + WaitT[i];
47+
TotalTT = TotalTT + TurnaroundT[i];
48+
}
49+
AverageWT = TotalWT/n;
50+
AverageTT = TotalTT/n;
51+
cout << "\t\t\t" << "burst time " <<" Waiting time " << " turnaround time" <<endl;
52+
for(i=0;i<n;i++)
53+
{
54+
cout << "Program of burst time " <<"\t "<< BurstT[i] << "\t\t" << WaitT[i] << "\t\t" << TurnaroundT[i] <<endl;
55+
}
56+
cout << "The average OF waiting time is " << AverageWT << "and turnaround time is " << AverageTT <<endl;
57+
return 0;
58+
}
59+
/*SAMPLE INPUT :
60+
61+
Enter the number of processes : 4
62+
The burst time for process 0 = 10
63+
The burst time for process 1 = 2
64+
The burst time for process 2 = 3
65+
The burst time for process 3 = 5
66+
67+
SAMPLE OUTPUT :
68+
69+
Sorted list in ascending order :
70+
Program with burst time 2
71+
Program with burst time 3
72+
Program with burst time 5
73+
Program with burst time 10
74+
Waiting time for process 0 = 0
75+
Turnaround time for process 0 = 2
76+
Waiting time for process 1 = 2
77+
Turnaround time for process 1 = 5
78+
Waiting time for process 2 = 5
79+
Turnaround time for process 2 = 10
80+
Waiting time for process 3 = 10
81+
Turnaround time for process 3 = 20
82+
burst time Waiting time turnaround time
83+
Program of burst time 2 0 2
84+
Program of burst time 3 2 5
85+
Program of burst time 5 5 10
86+
Program of burst time 10 10 20
87+
The average OF waiting time is 4.25and turnaround time is 9.25
88+
89+
TIME COMPLEXITY : O(n^2)
90+
SPACE COMPLEXITY : O(n)
91+
*/

0 commit comments

Comments
 (0)