-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombSort.cpp
More file actions
58 lines (53 loc) · 1.91 KB
/
CombSort.cpp
File metadata and controls
58 lines (53 loc) · 1.91 KB
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
#include <vector>
#include <string>
#include <algorithm>
#include "combSort.h"
#include "task.h"
#include "ternarySearch.h"
CombSort::CombSort(std::vector<Task>& tasks) : tasks(tasks), shrink(1.3), sorted(false), gap(0) {}
std::vector<Task> CombSort::sort_by_priority(std::vector<Task>& tasks) {
sorted = false;
gap = tasks.size();
while (!sorted) {
gap = static_cast<int>(gap / shrink);
if (gap <= 1) {
gap = 1;
sorted = true;
}
for (int i = 0; i + gap < tasks.size(); i++) {
if (tasks[i].getPriority() < tasks[i + gap].getPriority()) {
std::swap(tasks[i], tasks[i + gap]);
sorted = false;
}
}
}
return tasks;
}
std::vector<Task> CombSort::sort_by_date(std::vector<Task>& tasks) {
sorted = false;
gap = tasks.size();
while (!sorted) {
gap = static_cast<int>(gap / shrink);
if (gap <= 1) {
gap = 1;
sorted = true;
}
for (int i = 0; i + gap < tasks.size(); i++) {
int date1 = tasks[i].getDueDateInt();
int date2 = tasks[i + gap].getDueDateInt();
// Convert dates to yyyymmdd format
std::string convertedDate1 = "20" + std::to_string(date1 % 100) + std::to_string(date1 / 100 % 100) + std::to_string(date1 / 10000);
std::string convertedDate2 = "20" + std::to_string(date2 % 100) + std::to_string(date2 / 100 % 100) + std::to_string(date2 / 10000);
if (convertedDate1 > convertedDate2) {
std::swap(tasks[i], tasks[i + gap]);
sorted = false;
} else if (convertedDate1 == convertedDate2) {
if (tasks[i].getPriority() < tasks[i + gap].getPriority()) {
std::swap(tasks[i], tasks[i + gap]);
sorted = false;
}
}
}
}
return tasks;
}