-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.h
42 lines (35 loc) · 1.12 KB
/
sorting.h
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
// Comparison by ready times
bool byReadyTime(Process p1, Process p2, char direction = 'a') {
switch (direction) {
case 'a':
return p1.getReadyTime() > p2.getReadyTime();
break;
case 'd':
return p1.getReadyTime() < p2.getReadyTime();
break;
default:
return p1.getReadyTime() > p2.getReadyTime();
}
}
/* Sorting function for processes
* Implementation using Bubble Sort
*
* Function arguments:
* - Reference to the process list
* - Function pointer to the comparison function (see above)
*/
void sortProcesses (std::vector<Process>& proclist, bool (*compare)(Process, Process, char), char direction='a') {
int n=proclist.size()-1;
Process tmpProc; // Outsourcing process for swapping processes in Bubble Sort
// Bubble Sort
while (n >= 1) {
for (int a=0; a<n; a++) {
if (compare(proclist.at(a), proclist.at(a+1), direction)) {
tmpProc = proclist.at(a);
proclist[a] = proclist.at(a+1);
proclist[a+1] = tmpProc;
}
}
n--;
}
}