Skip to content

Commit 4c5a558

Browse files
Merge pull request DHEERAJHARODE#2619 from bhavyaGP/main
Create ShortestJobFirst(SJF).cpp
2 parents 0b746c8 + 2577901 commit 4c5a558

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void SJF(vector<pair<int, int>> processes)
5+
{
6+
int n = processes.size();
7+
int waiting_time[n];
8+
int turn_around_time[n];
9+
sort(processes.begin(), processes.end(), [](pair<int, int> a, pair<int, int> b)
10+
{ return a.second < b.second; });
11+
waiting_time[0] = 0;
12+
turn_around_time[0] = processes[0].second;
13+
for (int i = 1; i < n; i++)
14+
{
15+
waiting_time[i] = turn_around_time[i - 1];
16+
turn_around_time[i] = waiting_time[i] + processes[i].second;
17+
}
18+
cout << "PID\tBT\tWT\tTAT\n";
19+
for (int i = 0; i < n; i++)
20+
{
21+
cout << processes[i].first << "\t" << processes[i].second << "\t" << waiting_time[i] << "\t" << turn_around_time[i] << "\n";
22+
}
23+
}
24+
25+
26+
int main()
27+
{
28+
int n;
29+
cout << "Enter the number of processes: ";
30+
cin >> n;
31+
vector<pair<int, int>> processes(n);
32+
cout << "Enter the process ID and burst time for each process:\n";
33+
for (int i = 0; i < n; i++)
34+
{
35+
int pid, bt;
36+
cin >> pid >> bt;
37+
processes[i] = make_pair(pid, bt);
38+
}
39+
40+
cout << "SJF:\n\n";
41+
SJF(processes);
42+
cout<<endl;
43+
}

0 commit comments

Comments
 (0)