-
Notifications
You must be signed in to change notification settings - Fork 2
/
prioritySort.cpp
140 lines (120 loc) · 5.58 KB
/
prioritySort.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "task.h"
#include "taskList.h"
#include "prioritySort.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void prioritySort::differentMergeSorts(vector<Task>& vectorToBePrioritySorted) {
sortDueDate2(vectorToBePrioritySorted); //sorts by dueDate first
sortPriority2(vectorToBePrioritySorted); //sorts by priority second
}
void prioritySort::sortPriority2(vector<Task>& vectorToBePrioritySorted) {
int vectSize = vectorToBePrioritySorted.size();
int middle = vectSize / 2; //create left middle and right
vector<Task> left;
vector<Task> right;
left.reserve(middle);//make sure there is enough space in the left and right vectors without having to dynamically allocate
right.reserve(vectSize - middle);
copy(vectorToBePrioritySorted.begin(), vectorToBePrioritySorted.begin() + middle, back_inserter(left)); //copy into left vector
copy(vectorToBePrioritySorted.begin() + middle, vectorToBePrioritySorted.end(), back_inserter(right)); //copy into right vector
sortPriority2(left); //split left + right until only one block
sortPriority2(right);
int indexL = 0; //l in the front is hard to read so swapped position
int indexR = 0;
int indexOG = 0;
while (indexL < left.size() && indexR < right.size()) {//sort until one vector is empty
Task leftObj = left.at(indexL);
Task rightObj = right.at(indexR);
//means left = H | left = right | left = M right = L
if ((leftObj.getPriority() == 'H') || (leftObj.getPriority() == rightObj.getPriority()) || (leftObj.getPriority() == 'M' && rightObj.getPriority() == 'L')) {
vectorToBePrioritySorted.at(indexOG) = left.at(indexL);
++indexL;
}
else { //means right has greater priorty
vectorToBePrioritySorted.at(indexOG) = right.at(indexR);
++indexR;
}
++indexOG;
}
while (indexL < left.size()) { //if left vector not empty finish pushing into OG vector
vectorToBePrioritySorted.at(indexOG) = left.at(indexL);
++indexL;
++indexOG;
}
while (indexR < right.size()) { //if right vector not empty finish pushing into OG vector
vectorToBePrioritySorted.at(indexOG) = right.at(indexR);
++indexR;
++indexOG;
}
}
void prioritySort::sortDueDate2(vector<Task>& vectorToBePrioritySorted) {
if (vectorToBePrioritySorted.size() <= 1) {//base case stop when each block is of size = 1
return; //nothing to be sorted
}
int vectSize = vectorToBePrioritySorted.size();
int middle = vectSize / 2; //create left middle and right
vector<Task> left;
vector<Task> right;
left.reserve(middle);//make sure there is enough space in the left and right vectors without having to dynamically allocate
right.reserve(vectSize - middle);
copy(vectorToBePrioritySorted.begin(), vectorToBePrioritySorted.begin() + middle, back_inserter(left)); //copy into left vector
copy(vectorToBePrioritySorted.begin() + middle, vectorToBePrioritySorted.end(), back_inserter(right)); //copy into right vector
sortDueDate2(left); //split left + right until only one block
sortDueDate2(right);
int indexL = 0; //l in the front is hard to read so swapped position
int indexR = 0;
int indexOG = 0;
int i = 2;
while (indexL < left.size() && indexR < right.size()) {//sort until one vector is empty
Task leftObj = left.at(indexL);
Task rightObj = right.at(indexR);
leftObj.compactDueDateVector();
rightObj.compactDueDateVector();
vector<int> leftObjDateVect = leftObj.getNumsOfDueDateVector();
vector<int> rightObjDateVect = rightObj.getNumsOfDueDateVector();
if (i >= 0) {
if (leftObjDateVect.at(i) < rightObjDateVect.at(i)) {//leftObj's dueDate is closer than rightObj's dueDate
vectorToBePrioritySorted.at(indexOG) = left.at(indexL);
++indexL;
++indexOG;
}
else if (leftObjDateVect.at(i) > rightObjDateVect.at(i)) {//rightObj's dueDate is closer than leftObj's dueDate
vectorToBePrioritySorted.at(indexOG) = right.at(indexR);
++indexR;
++indexOG;
}
}
else if (i <= 0) {
if (leftObj.getDueDateHour() < rightObj.getDueDateHour()) {//left and rightObj have the same year/month/day so compare time
vectorToBePrioritySorted.at(indexOG) = left.at(indexL);
++indexL;
}
else if (leftObj.getDueDateHour() > rightObj.getDueDateHour()){//rightObj dueDateHour is closer than leftObj dueDateHour
vectorToBePrioritySorted.at(indexOG) = right.at(indexR);
++indexR;
}
else if (indexL < left.size() && !(indexR < left.size())) {
vectorToBePrioritySorted.at(indexOG) = right.at(indexR);
++indexR;
}
else if (!(indexL < left.size()) && indexR < left.size()) {
vectorToBePrioritySorted.at(indexOG) = left.at(indexL);
++indexL;
}
++indexOG;
}
--i;
}
while (indexL < left.size()) { //if left vector not empty finish pushing into OG vector
vectorToBePrioritySorted.at(indexOG) = left.at(indexL);
++indexL;
++indexOG;
}
while (indexR < right.size()) { //if right vector not empty finish pushing into OG vector
vectorToBePrioritySorted.at(indexOG) = right.at(indexR);
++indexR;
++indexOG;
}
}