-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPmergeMe.cpp
202 lines (167 loc) · 4.58 KB
/
PmergeMe.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wcorrea- <wcorrea-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/04 19:27:41 by wcorrea- #+# #+# */
/* Updated: 2023/09/04 19:27:42 by wcorrea- ### ########.fr */
/* */
/* ************************************************************************** */
#include "PmergeMe.hpp"
// --------------------------------------- common functions ---------------------------------------
bool isValidNumber(std::string const &in)
{
double nb = std::atof(in.c_str());
if (in.empty() || (in.find_first_not_of(VALID_SET) != std::string::npos)
|| nb < 0 || nb > (double)INT_MAX)
return (false);
return (true);
}
bool isValidInput(char **av)
{
for (int i = 1; av[i]; i++)
if (!isValidNumber(av[i]))
return (false);
return (true);
}
void printBefore(char **av)
{
std::cout << "Before:\t";
for (int i = 0; av[i]; i++)
std::cout << av[i] << " ";
std::cout << std::endl;
}
// --------------------------------------- list functions ---------------------------------------
void fillList(std::list<int> &list, char **av)
{
for (int i = 0; av[i]; i++)
list.push_back(std::atoi(av[i]));
}
void listInsert(std::list<int> &sorted, int const &nb)
{
std::list<int>::iterator it;
for (it = sorted.begin(); it != sorted.end(); it++)
{
if (*it > nb)
{
sorted.insert(it, nb);
return ;
}
}
sorted.insert(it, nb);
}
void listInsertionSort(std::list<int> &list)
{
std::list<int> sorted;
while (!list.empty())
{
int nb = list.front();
list.pop_front();
listInsert(sorted, nb);
}
list.swap(sorted);
}
void listMergeInsertionSort(std::list<int> &list)
{
if (list.size() <= LIMITER)
{
listInsertionSort(list);
return ;
}
std::list<int> left, right;
int middle = list.size() / 2;
for (int i = 0; i < middle; i++)
{
left.push_back(list.front());
list.pop_front();
}
right = list;
listMergeInsertionSort(left);
listMergeInsertionSort(right);
list.clear();
std::merge(left.begin(), left.end(), right.begin(), right.end(), std::back_inserter(list));
}
void sortList(std::list<int> &list, char **av, double &time)
{
clock_t start = clock();
fillList(list, av);
listMergeInsertionSort(list);
clock_t end = clock();
time = (double)(end - start) / ((double)CLOCKS_PER_SEC / 1000);
}
void printListAfter(std::list<int> &list)
{
std::list<int>::const_iterator it;
std::cout << "After:\t";
for (it = list.begin(); it != list.end(); it++)
std::cout << *it << " ";
std::cout << std::endl;
}
// --------------------------------------- deque functions ---------------------------------------
void fillDeque(std::deque<int> &deque, char **av)
{
for (int i = 0; av[i]; i++)
deque.push_back(std::atoi(av[i]));
}
void printDequeAfter(std::deque<int> &deque)
{
std::deque<int>::const_iterator it;
std::cout << "After:\t";
for (it = deque.begin(); it != deque.end(); it++)
std::cout << *it << " ";
std::cout << std::endl;
}
void dequeInsert(std::deque<int> &sorted, int const &nb)
{
std::deque<int>::iterator it;
for (it = sorted.begin(); it != sorted.end(); it++)
{
if (*it > nb)
{
sorted.insert(it, nb);
return ;
}
}
sorted.insert(it, nb);
}
void dequeInsertionSort(std::deque<int> &deque)
{
std::deque<int> sorted;
while (!deque.empty())
{
int nb = deque.front();
deque.pop_front();
dequeInsert(sorted, nb);
}
deque.swap(sorted);
}
void dequeMergeInsertionSort(std::deque<int> &deque)
{
if (deque.size() <= LIMITER)
{
dequeInsertionSort(deque);
return ;
}
std::deque<int> left, right;
int middle = deque.size() / 2;
for (int i = 0; i < middle; i++)
{
left.push_back(deque.front());
deque.pop_front();
}
right = deque;
dequeMergeInsertionSort(left);
dequeMergeInsertionSort(right);
deque.clear();
std::merge(left.begin(), left.end(), right.begin(), right.end(), std::back_inserter(deque));
}
void sortDeque(std::deque<int> &deque, char **av, double &time)
{
clock_t start = clock();
fillDeque(deque, av);
dequeMergeInsertionSort(deque);
clock_t end = clock();
time = (double)(end - start) / ((double)CLOCKS_PER_SEC / 1000);
}