-
Notifications
You must be signed in to change notification settings - Fork 1
/
SSTF-Disk-Scheduling.cpp
90 lines (77 loc) · 2.06 KB
/
SSTF-Disk-Scheduling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// C++ program to demonstrate
// SSTF Disk Scheduling algorithm
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, k, n, m, sum = 0, head;
cout << "\nEnter the size of disk : ";
cin >> m;
cout << "\nEnter number of requests : ";
cin >> n;
cout << "\nEnter the requests:\n";
//creating two arrays, array a will store the input
//I/O requests and array b will store the output
vector<int> a(n), b;
//creating a map to store the count of each element
//in the array a.
map<int, int> mp;
for (i = 0; i < n; i++)
{
cin >> a[i];
mp[a[i]]++;
}
for (i = 0; i < n; i++)
{
if (a[i] > m)
{
cout << "Error, Wrong Track No. " << a[i] << "\n";
return 0;
}
}
cout << "\nEnter the head position : ";
cin >> head;
int temp = head;
int ele;
b.push_back(head);
int count = 0;
while (count < n)
{
//initially taking diff to be very large.
int diff = 999999;
//traversing in map to find the least difference
for (auto q : mp)
{
if (abs(q.first - temp) < diff)
{
ele = q.first;
diff = abs(q.first - temp);
}
}
//deleting the element that has the least
//difference from the map
mp[ele]--;
if (mp[ele] == 0)
{
mp.erase(ele);
}
//adding that element to our output array.
b.push_back(ele);
temp = ele;
count++;
}
cout << "\nSeek Sequence of tracks is " << endl;
//printing the output array
cout << b[0]; //head
temp = b[0];
for (i = 1; i < b.size(); i++)
{
cout << " -> " << b[i];
sum += abs(b[i] - temp);
temp = b[i];
}
cout << '\n';
cout << "\nTotal number of seek operations = " << sum << '\n';
cout << "Average head movement = " << (float)sum / n << '\n';
return 0;
}