-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBucketSort.cpp
161 lines (143 loc) · 7.99 KB
/
BucketSort.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "BucketSort.h"
void BucketSort::bucketSort(std::vector<std::unique_ptr<Citation>> &citations, unsigned int recursionDepth) {
// Using std::unordered_map here so that we can implement the real "internal" sorting algorithm ourselves.
// It would be more efficient to use std::map, but that would not meet the requirements of the project.
std::unordered_map<unsigned char, std::vector<std::unique_ptr<Citation>>> buckets;
// Linked list to store the keys in the correctly sorted order.
std::list<unsigned char> sortedKeys;
// Insert '\0' as the first element, as it's guaranteed to be in the unordered_map (see next statement)
sortedKeys.insert(sortedKeys.begin(), '\0');
// Special bucket to store strings that have terminated.
buckets.insert(std::pair('\0', std::vector<std::unique_ptr<Citation>>()));
// Loop over input vector, sorting into each bucket
for (std::unique_ptr<Citation>& citationPtr : citations) {
if (recursionDepth >= citationPtr->plateNumber.size()) {
// We've reached the end of the string.
buckets.at('\0').push_back(std::make_unique<Citation>(*citationPtr));
}
else {
unsigned char bucketKey = citationPtr->plateNumber.at(recursionDepth);
if (buckets.count(bucketKey) == 0) {
// Create new bucket if the old bucket is not present.
buckets.insert(std::pair(bucketKey, std::vector<std::unique_ptr<Citation>>()));
// Perform a pseudo-insertion sort so that we know the correct order of the keys.
auto currentElementIterator = sortedKeys.begin();
while (true) {
if (bucketKey < *currentElementIterator) {
// Insert the key before the current element.
sortedKeys.insert(currentElementIterator, bucketKey);
break;
}
if (currentElementIterator == sortedKeys.end()) {
// We've reached the end and still haven't found a suitable place to insert.
// So the element must belong at the end.
sortedKeys.insert(currentElementIterator, bucketKey);
break;
}
// Increment the iterator
currentElementIterator++;
}
}
buckets.at(bucketKey).push_back(std::make_unique<Citation>(*citationPtr));
}
}
// "Base case" - terminate if we reach the end of all strings in the bucket
if (buckets.at('\0').size() == citations.size()) {
// We've reached the end of all the strings.
return;
}
// Sort all the buckets
for (auto& bucketPair : buckets) {
if (bucketPair.first == '\0') continue; // Skip the "end of string" bucket, as there's nothing to sort there.
bucketSort(bucketPair.second, recursionDepth + 1); // Bucket sort the smaller bucket.
}
// Concatenate all the buckets. All the buckets in "buckets" map should be sorted now
unsigned int originalVectorIndex = 0;
for (const unsigned char& bucketKey : sortedKeys) {
// Get the keys in order from sortedKeys
std::vector<std::unique_ptr<Citation>>& currentBucket = buckets.at(bucketKey);
for (const std::unique_ptr<Citation>& currentCitationPtr : currentBucket) {
// Copy the new sorted Citations from each bucket to the old vector
citations.at(originalVectorIndex) = std::make_unique<Citation>(*currentCitationPtr);
originalVectorIndex++;
}
if (originalVectorIndex > citations.size()) {
throw std::logic_error("Invalid state: originalVectorIndex = " + std::to_string(originalVectorIndex) +
"; citations.size()=" + std::to_string(citations.size()));
}
}
}
void BucketSort::bucketSortDate(std::vector<std::unique_ptr<Citation>>& citations, unsigned int recursionDepth) {
// Using std::unordered_map here so that we can implement the real "internal" sorting algorithm ourselves.
// It would be more efficient to use std::map, but that would not meet the requirements of the project.
std::unordered_map<unsigned char, std::vector<std::unique_ptr<Citation>>> buckets;
// Linked list to store the keys in the correctly sorted order.
std::list<unsigned char> sortedKeys;
// Insert '\0' as the first element, as it's guaranteed to be in the unordered_map (see next statement)
sortedKeys.insert(sortedKeys.begin(), '\0');
// Special bucket to store strings that have terminated.
buckets.insert(std::pair('\0', std::vector<std::unique_ptr<Citation>>()));
// Loop over input vector, sorting into each bucket
for (std::unique_ptr<Citation>& citationPtr : citations) {
if (recursionDepth >= citationPtr->dateTime->getDateTimeString().size()) {
// We've reached the end of the string.
buckets.at('\0').push_back(std::make_unique<Citation>(*citationPtr));
}
else {
unsigned char bucketKey = citationPtr->dateTime->getDateTimeString().at(recursionDepth);
if (buckets.count(bucketKey) == 0) {
// Create new bucket if the old bucket is not present.
buckets.insert(std::pair(bucketKey, std::vector<std::unique_ptr<Citation>>()));
// Perform a pseudo-insertion sort so that we know the correct order of the keys.
auto currentElementIterator = sortedKeys.begin();
while (true) {
if (bucketKey < *currentElementIterator) {
// Insert the key before the current element.
sortedKeys.insert(currentElementIterator, bucketKey);
break;
}
if (currentElementIterator == sortedKeys.end()) {
// We've reached the end and still haven't found a suitable place to insert.
// So the element must belong at the end.
sortedKeys.insert(currentElementIterator, bucketKey);
break;
}
// Increment the iterator
currentElementIterator++;
}
}
buckets.at(bucketKey).push_back(std::make_unique<Citation>(*citationPtr));
}
}
// "Base case" - terminate if we reach the end of all strings in the bucket
if (buckets.at('\0').size() == citations.size()) {
// We've reached the end of all the strings.
return;
}
// Sort all the buckets
for (auto& bucketPair : buckets) {
if (bucketPair.first == '\0') continue; // Skip the "end of string" bucket, as there's nothing to sort there.
bucketSortDate(bucketPair.second, recursionDepth + 1); // Bucket sort the smaller bucket.
}
// Concatenate all the buckets. All the buckets in "buckets" map should be sorted now
unsigned int originalVectorIndex = 0;
for (const unsigned char& bucketKey : sortedKeys) {
// Get the keys in order from sortedKeys
std::vector<std::unique_ptr<Citation>>& currentBucket = buckets.at(bucketKey);
for (const std::unique_ptr<Citation>& currentCitationPtr : currentBucket) {
// Copy the new sorted Citations from each bucket to the old vector
citations.at(originalVectorIndex) = std::make_unique<Citation>(*currentCitationPtr);
originalVectorIndex++;
}
if (originalVectorIndex > citations.size()) {
throw std::logic_error("Invalid state: originalVectorIndex = " + std::to_string(originalVectorIndex) +
"; citations.size()=" + std::to_string(citations.size()));
}
}
}
void BucketSort::bucketSortWrapper(std::vector<std::unique_ptr<Citation>> &citations) {
bucketSort(citations, 0);
}
void BucketSort::bucketSortDateWrapper(std::vector<std::unique_ptr<Citation>>& citations) {
bucketSortDate(citations, 0);
}