Skip to content

Commit 4bb4162

Browse files
Merge pull request #160 from ardika99/master
Adds sample i/o and time and space complexity
2 parents 5698318 + 016c65b commit 4bb4162

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

C++/SJF(Scheduling algo.)/Readme.md

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+
*/
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main()
5+
{
6+
int i,n,WaitT[20],BurstT[20],TurnaroundT[20],j,key,Priority[20],ProcessID[20];
7+
float AverageTT,AvgWT,TotalWt=0,TotalTT=0;
8+
printf("enter the number of processes : ");
9+
scanf("%d",&n);
10+
printf("Assign the process and burst time\n");
11+
for(i=0;i<n;i++)
12+
{
13+
printf("the process id, burst time and priority for process");
14+
scanf("%d\t%d\t%d",&ProcessID[i],&BurstT[i],&Priority[i]);
15+
}
16+
for (i=0;i<n;i++)
17+
{
18+
for(j=0;j<n;j++)
19+
if(Priority[i]<Priority[j] )
20+
{
21+
key = Priority[i];
22+
Priority[i] = Priority[j];
23+
Priority[j] = key;
24+
key = BurstT[i];
25+
BurstT[i] = BurstT[j];
26+
BurstT[j] = key;
27+
key = ProcessID[i];
28+
ProcessID[i] = ProcessID[j];
29+
ProcessID[j] = key;
30+
31+
}
32+
}
33+
printf("process_name\tburst_time\tpriority");
34+
for (i=0;i<n;i++)
35+
{
36+
printf("\n%3d\t%13d\t%13d",ProcessID[i],BurstT[i],Priority[i]);
37+
}
38+
WaitT[-1]=0;
39+
BurstT[-1]=0;
40+
WaitT[0]=0;
41+
TurnaroundT[0]=BurstT[0];
42+
for(i=0;i<n;i++)
43+
{
44+
WaitT[i]=WaitT[i-1]+BurstT[i-1];
45+
printf("\nwaiting time for process %d = %d \n",i,WaitT[i]);
46+
TurnaroundT[i]=WaitT[i]+BurstT[i];
47+
printf("turnout time for process %d = %d \n",i,TurnaroundT[i]);
48+
}
49+
for(i=0;i<n;i++)
50+
{
51+
TotalWt = TotalWt + WaitT[i];
52+
TotalTT = TotalTT + TurnaroundT[i];
53+
}
54+
AvgWT = TotalWt/n;
55+
AverageTT = TotalTT/n;
56+
for(i=0;i<n;i++)
57+
{
58+
printf("program of burst time %d %d %d\n",BurstT[i],WaitT[i],TurnaroundT[i]);
59+
}
60+
printf("the average of waiting time is %f and turnout time is %f \n",AvgWT,AverageTT);
61+
return 0;
62+
}
63+
/*SAMPLE INPUT :
64+
65+
enter the number of processes : 5
66+
Assign the process and burst time
67+
the process id, burst time and priority for process1 10 3
68+
the process id, burst time and priority for process2 1 1
69+
the process id, burst time and priority for process3 2 4
70+
the process id, burst time and priority for process4 1 5
71+
the process id, burst time and priority for process5 5 2
72+
73+
SAMPLE OUTPUT :
74+
75+
process_name burst_time priority
76+
2 1 1
77+
5 5 2
78+
1 10 3
79+
3 2 4
80+
4 1 5
81+
waiting time for process 0 = 0
82+
turnout time for process 0 = 1
83+
84+
waiting time for process 1 = 1
85+
turnout time for process 1 = 6
86+
87+
waiting time for process 2 = 6
88+
turnout time for process 2 = 16
89+
90+
waiting time for process 3 = 16
91+
turnout time for process 3 = 18
92+
93+
waiting time for process 4 = 18
94+
turnout time for process 4 = 19
95+
program of burst time 1 0 1
96+
program of burst time 5 1 6
97+
program of burst time 10 6 16
98+
program of burst time 2 16 18
99+
program of burst time 1 18 19
100+
the average of waiting time is 8.200000 and turnout time is 12.000000
101+
102+
TIME COMPLEXITY : O(n^2)
103+
SPACE COMPLEXITY : O(n)
104+
*/

C/Priority scheduling/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Priority based Scheduling (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 priority queue algorithm. First sort
8+
the processes in ascending order of their priority time and the process having the shortest priority
9+
time is executed first and so on. Waiting time of the first process is kept zero and the waiting time of the second process is the burst time of the first process and the waiting time of the third process is the sum of the burst time of the first and the second process and so on. After calculating all the waiting times the average waiting time is calculated as the average of all the waiting times.

0 commit comments

Comments
 (0)