-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (56 loc) · 2.19 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
69
70
71
72
#include "PmergeMe.hpp"
#include "PmergeMe.hpp"
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]));
std::clock_t start = std::clock();
std::vector<int> sorted_data = ford_johnson_sort(pairs, straggler, has_straggler);
std::clock_t end = std::clock();
double time1 = (double)(end - start) * 1000000.0 / CLOCKS_PER_SEC;
/* start = std::clock(); */
/* std::deque<int> sorted_deque = ford_johnson_sort_deque(deque_pairs, straggler, has_straggler); */
/* end = std::clock(); */
/* double time2 = (double)(end - start) * 1000000.0 / CLOCKS_PER_SEC; */
check_if_sorted(sorted_data);
/* check_if_sorted_deque(sorted_deque); */
std::cout << "Time for first sort (vector): " << time1 << " us" << std::endl;
/* std::cout << "Time for second sort (deque): " << time2 << " us" << std::endl; */
return 0;
}