-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (56 loc) · 2.05 KB
/
main.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
#include "PmergeMe.hpp"
#include <chrono>
int main(int argc, char* argv[])
{
std::vector<int> data;
std::deque<int> data2;
if (argc <= 2 || !std::isdigit(argv[1][0]) || !std::isdigit(argv[2][0])){
std::cout << "Usage: " << argv[0] << " <number1> <number2> ... <numberN>" << std::endl;
std::cout << " " << std::endl;
std::cout << "Example: " << argv[0] << " `shuf -i 1-N -n N | tr \"\\n\" \" \"`" << std::endl;
return 0;
}
for (int i = 1; i < argc; ++i) {
std::string argument(argv[i]);
if (argument.empty()) {
std::cout << "Invalid argument: " << argument << std::endl;
return 0;
}
data.push_back(std::atoi(argument.c_str()));
data2.push_back(std::atoi(argument.c_str()));
}
if (data.empty() || data2.empty()) {
std::cout << "Nothing to sort" << std::endl;
return 0;
}
int n = data.size();
int straggler = -1;
bool has_straggler = false;
if (n % 2 != 0) {
has_straggler = true;
straggler = data.back();
data.pop_back();
data2.pop_back();
n--;
}
std::vector<std::pair<int, int>> pairs;
pairs.reserve(n / 2);
for (int i = 0; i < n; i += 2)
pairs.push_back(std::make_pair(data[i], data[i + 1]));
std::deque<std::pair<int, int>> deque_pairs;
for (int i = 0; i < n; i += 2)
deque_pairs.push_back(std::make_pair(data2[i], data2[i + 1]));
// Remplacement de clock() par chrono
auto start = std::chrono::high_resolution_clock::now();
std::vector<int> sorted_data = ford_johnson_sort(pairs, straggler, has_straggler);
auto end = std::chrono::high_resolution_clock::now();
double time1 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "Sorted numbers: ";
for (int num : sorted_data) {
std::cout << num << " ";
}
std::cout << std::endl;
check_if_sorted(sorted_data);
std::cout << "Time for first sort (vector): " << time1 << " µs" << std::endl;
return 0;
}