Skip to content

Commit b4186a9

Browse files
committed
improve sorting algorithm
1 parent 4b64984 commit b4186a9

2 files changed

Lines changed: 87 additions & 105 deletions

File tree

module 09/ex02/PmergeMe.cpp

Lines changed: 72 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -54,69 +54,53 @@ bool PmergeMe::parseArgs(int argc, char **argv)
5454
return true;
5555
}
5656

57-
void PmergeMe::printSeq(const std::vector<int> &seq, const std::string &label)
58-
{
59-
std::cout << label << ": ";
60-
61-
for (size_t i = 0; i < seq.size(); i++)
62-
{
63-
if (i > 0)
64-
std::cout << " ";
65-
std::cout << seq[i];
66-
}
67-
68-
std::cout << std::endl;
69-
}
70-
71-
std::vector<int> PmergeMe::getJacob(int n)
57+
std::vector<int> PmergeMe::getJacob(size_t size)
7258
{
7359
std::vector<int> jacobsthal;
74-
7560
jacobsthal.push_back(0);
76-
if (n > 0)
77-
jacobsthal.push_back(1);
78-
79-
for (int i = 2; i <= n; i++)
61+
jacobsthal.push_back(1);
62+
63+
while (jacobsthal.back() < static_cast<int>(size))
8064
{
81-
int next = jacobsthal[i - 1] + 2 * jacobsthal[i - 2];
65+
int lastNum = jacobsthal.back();
66+
int secondLastNum = jacobsthal[jacobsthal.size() - 2];
67+
68+
int next = lastNum + 2 * secondLastNum;
8269
jacobsthal.push_back(next);
8370
}
84-
71+
8572
return jacobsthal;
8673
}
8774

88-
std::vector<int> PmergeMe::getInsertPos(std::vector<int> &jacobSeq, size_t pairsSize)
75+
76+
std::vector<int> PmergeMe::getInsertPos(std::vector<int> &jacobSeq, size_t size)
8977
{
90-
std::vector<int> insertionOrder;
91-
std::vector<bool> inserted(pairsSize, false);
92-
inserted[0] = true;
78+
std::vector<int> order;
79+
std::vector<bool> used(size, false);
80+
used[0] = true;
9381

94-
for (size_t i = 1; i < jacobSeq.size(); i++)
82+
for (size_t i = 1; i < jacobSeq.size() && jacobSeq[i] < static_cast<int>(size); i++)
9583
{
96-
int idx = jacobSeq[i];
97-
if (idx < (int)pairsSize && !inserted[idx])
98-
{
99-
insertionOrder.push_back(idx);
100-
inserted[idx] = true;
84+
if (!used[jacobSeq[i]]) {
85+
order.push_back(jacobSeq[i]);
86+
used[jacobSeq[i]] = true;
10187
}
102-
103-
for (int j = idx - 1; j > jacobSeq[i - 1]; j--)
104-
{
105-
if (j >= 0 && j < (int)pairsSize && !inserted[j])
106-
{
107-
insertionOrder.push_back(j);
108-
inserted[j] = true;
88+
89+
for (int j = jacobSeq[i] - 1; j > jacobSeq[i-1]; j--) {
90+
if (j < (int)size && !used[j]) {
91+
order.push_back(j);
92+
used[j] = true;
10993
}
11094
}
11195
}
11296

113-
for (size_t i = 1; i < pairsSize; i++)
114-
{
115-
if (!inserted[i])
116-
insertionOrder.push_back(i);
97+
for (size_t i = 1; i < size; i++) {
98+
if (!used[i]) {
99+
order.push_back(i);
100+
}
117101
}
118102

119-
return insertionOrder;
103+
return order;
120104
}
121105

122106
void PmergeMe::sortVector()
@@ -139,15 +123,11 @@ void PmergeMe::sortVector()
139123
{
140124
int first = _vec[i];
141125
int second = _vec[i + 1];
126+
if (first < second)
127+
std::swap(first, second);
142128
pairs.push_back(std::make_pair(first, second));
143129
}
144130

145-
for (size_t i = 0; i < pairs.size(); i++)
146-
{
147-
if (pairs[i].first < pairs[i].second)
148-
std::swap(pairs[i].first, pairs[i].second);
149-
}
150-
151131
std::vector<int> mainChain;
152132
for (size_t i = 0; i < pairs.size(); i++)
153133
{
@@ -159,33 +139,35 @@ void PmergeMe::sortVector()
159139
_vec = mainChain;
160140
sortVector();
161141
mainChain = _vec;
162-
_vec.clear();
163142
}
164143

165-
std::vector<int> result;
166-
if (!mainChain.empty())
167-
result.push_back(mainChain[0]);
144+
std::vector<int> result = mainChain;
168145

169-
if (!pairs.empty())
170-
result.insert(result.begin(), pairs[0].second);
146+
std::vector<int> pendChain;
147+
for (size_t i = 0; i < pairs.size(); i++)
148+
{
149+
pendChain.push_back(pairs[i].second);
150+
}
171151

172-
if (pairs.size() > 1)
152+
if (!pendChain.empty())
173153
{
174-
int jacobsthalSize = 3;
175-
while (getJacob(jacobsthalSize).back() < (int)pairs.size())
176-
jacobsthalSize++;
154+
std::vector<int>::iterator pos = std::lower_bound(result.begin(), result.end(), pendChain[0]);
155+
result.insert(pos, pendChain[0]);
156+
}
177157

178-
std::vector<int> jacobSeq = getJacob(jacobsthalSize);
179-
std::vector<int> insertionOrder = getInsertPos(jacobSeq, pairs.size());
158+
if (pendChain.size() > 1)
159+
{
160+
std::vector<int> jacobSeq = getJacob(pendChain.size());
161+
std::vector<int> insertionOrder = getInsertPos(jacobSeq, pendChain.size());
180162

181163
for (size_t i = 0; i < insertionOrder.size(); i++)
182164
{
183165
int idx = insertionOrder[i];
184-
std::vector<int>::iterator pos = std::lower_bound(result.begin(), result.end(), mainChain[idx]);
185-
result.insert(pos, mainChain[idx]);
186-
187-
pos = std::lower_bound(result.begin(), result.end(), pairs[idx].second);
188-
result.insert(pos, pairs[idx].second);
166+
if (idx > 0 && idx < (int)pendChain.size())
167+
{
168+
std::vector<int>::iterator pos = std::lower_bound(result.begin(), result.end(), pendChain[idx]);
169+
result.insert(pos, pendChain[idx]);
170+
}
189171
}
190172
}
191173

@@ -218,15 +200,11 @@ void PmergeMe::sortDeque()
218200
{
219201
int first = _deq[i];
220202
int second = _deq[i + 1];
203+
if (first < second)
204+
std::swap(first, second);
221205
pairs.push_back(std::make_pair(first, second));
222206
}
223207

224-
for (size_t i = 0; i < pairs.size(); i++)
225-
{
226-
if (pairs[i].first < pairs[i].second)
227-
std::swap(pairs[i].first, pairs[i].second);
228-
}
229-
230208
std::deque<int> mainChain;
231209
for (size_t i = 0; i < pairs.size(); i++)
232210
{
@@ -238,34 +216,36 @@ void PmergeMe::sortDeque()
238216
_deq = mainChain;
239217
sortDeque();
240218
mainChain = _deq;
241-
_deq.clear();
242219
}
243220

244-
std::deque<int> result;
245-
if (!mainChain.empty())
246-
result.push_back(mainChain.front());
221+
std::deque<int> result = mainChain;
247222

248-
if (!pairs.empty())
249-
result.push_front(pairs[0].second);
223+
std::deque<int> pendChain;
224+
for (size_t i = 0; i < pairs.size(); i++)
225+
{
226+
pendChain.push_back(pairs[i].second);
227+
}
250228

251-
if (pairs.size() > 1)
229+
if (!pendChain.empty())
252230
{
253-
int jacobsthalSize = 3;
254-
while (getJacob(jacobsthalSize).back() < (int)pairs.size())
255-
jacobsthalSize++;
231+
std::deque<int>::iterator pos = std::lower_bound(result.begin(), result.end(), pendChain[0]);
232+
result.insert(pos, pendChain[0]);
233+
}
256234

257-
std::vector<int> jacobSeq = getJacob(jacobsthalSize);
258-
std::vector<int> insertionOrder = getInsertPos(jacobSeq, pairs.size());
235+
if (pendChain.size() > 1)
236+
{
237+
std::vector<int> jacobSeq = getJacob(pendChain.size());
238+
std::vector<int> insertionOrder = getInsertPos(jacobSeq, pendChain.size());
259239

260240
for (size_t i = 0; i < insertionOrder.size(); i++)
261241
{
262242
int idx = insertionOrder[i];
263-
264-
std::deque<int>::iterator pos = std::lower_bound(result.begin(), result.end(), mainChain[idx]);
265-
result.insert(pos, mainChain[idx]);
266-
267-
pos = std::lower_bound(result.begin(), result.end(), pairs[idx].second);
268-
result.insert(pos, pairs[idx].second);
243+
if (idx > 0 && idx < (int)pendChain.size())
244+
{
245+
int elementToInsert = pendChain[idx];
246+
std::deque<int>::iterator pos = std::lower_bound(result.begin(), result.end(), elementToInsert);
247+
result.insert(pos, elementToInsert);
248+
}
269249
}
270250
}
271251

@@ -292,16 +272,6 @@ void PmergeMe::sort()
292272
clock_t deqEnd = clock();
293273
double deqTime = static_cast<double>(deqEnd - deqStart) / CLOCKS_PER_SEC * 1000000;
294274

295-
bool isSorted = true;
296-
for (size_t i = 1; i < _vec.size(); i++) {
297-
if (_vec[i] < _vec[i-1]) {
298-
std::cout << _vec[i] << std::endl;
299-
std::cout << _vec[i - 1] << std::endl;
300-
isSorted = false;
301-
break;
302-
}
303-
}
304-
305275
printSeq(original, "Before");
306276
printSeq(_vec, "After");
307277

@@ -312,4 +282,4 @@ void PmergeMe::sort()
312282
std::cout << "Time to process a range of " << _deq.size()
313283
<< " elements with std::deque : " << std::fixed << std::setprecision(5)
314284
<< deqTime << " us" << std::endl;
315-
}
285+
}

module 09/ex02/PmergeMe.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@ class PmergeMe
2020
std::deque<int> _deq;
2121

2222
bool isValidNumber(const char *str);
23-
void printSeq(const std::vector<int> &seq, const std::string &label);
24-
std::vector<int> getJacob(int n);
25-
std::vector<int> getInsertPos(std::vector<int> &jacobSeq, size_t pairsSize);
23+
std::vector<int> getJacob(size_t size);
24+
std::vector<int> getInsertPos(std::vector<int> &jacobSeq, size_t size);
2625

2726
void sortVector();
2827
void sortDeque();
2928

29+
template<typename T>
30+
void printSeq(const T &seq, const std::string &label)
31+
{
32+
std::cout << label << ": ";
33+
for (size_t i = 0; i < seq.size(); i++)
34+
{
35+
if (i > 0)
36+
std::cout << " ";
37+
std::cout << seq[i];
38+
}
39+
std::cout << std::endl;
40+
}
41+
3042
public:
3143
PmergeMe();
3244
PmergeMe(const PmergeMe &other);

0 commit comments

Comments
 (0)