-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
152 lines (122 loc) · 4.69 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
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
#include <algorithm>
#include <getopt.h>
#include <iostream>
#include <stdexcept>
#include <string>
#include <memory>
#include <map>
#include <vector>
#include <fstream>
#include "SortAlgorithm.h"
#include "FileHandler.h"
using std::cout;
using std::string;
using std::vector;
using sorting::SortAlgorithm;
using sorting::SortingMethod;
const int INTEGER_TYPE = 1;
const int STRING_TYPE = 2;
string INPUT_FILE_PATH;
string OUTPUT_FILE_PATH;
/**
* Sorts the data and writes the sorting method, input size, and execution time to the output file
* @param data The data to be sorted
* @param sorter The sorting algorithm to be used
* @param outputFileHandler The FileHandler object to write the output file
* @param size The size of the input data
*/
template <typename T>
void sortAndWrite(vector<T>& data, SortAlgorithm<T>& sorter, FileHandler& outputFileHandler, int size) {
cout << "Starting sort...\n";
int time, n = 2;
const vector<T> copyData(data); // Create a copy of the data to be sorted
// Run the sorting algorithm multiple times, in fast algorithms, to get a more accurate execution time
if (sorter.getSortingMethod() == SortingMethod::ShellSort || sorter.getSortingMethod() == SortingMethod::MergeSort || sorter.getSortingMethod() == SortingMethod::QuickSort)
n = 10;
for (int i = 0; i < n; i++) {
time += sorter.sort(data, size);
data = copyData;
}
time /= n;
cout << "Execution Time: " << time << " miliseconds" << '\n';
// Write the sorting method, input size, and execution time to the output file
outputFileHandler.write(
{sorter.getSortingMethodName() + "," + std::to_string(size) + "," + std::to_string(time)},
true
);
}
/**
* Runs all sorting algorithms on the given data
* @param inputFileHandler The FileHandler object to read the input file
* @param outputFileHandler The FileHandler object to write the output file
*/
template <typename T>
void runAllSortAlgorithms(FileHandler& inputFileHandler, FileHandler& outputFileHandler) {
SortAlgorithm<T> sorter;
vector<T> data;
int total = inputFileHandler.getSize();
// Increment the size of the input data by 5% each iteration, 20 executions in total
int increment = total / 20;
data.reserve(total);
cout << "Total: " << total << '\n';
cout << "Increment: " << increment << '\n';
// Write the header row to the file
outputFileHandler.write({"Sorting Method,Input Size,Execution Time"}, false);
// Get all sorting algorithms
auto algorithms = sorter.getAlgorithms();
for (const auto& it : algorithms) {
auto method = it.first;
sorter.setSortingMethod(method);
cout << "Sorting Method: " << sorter.getSortingMethodName() << '\n';
for (int size = increment; size <= total; size += increment) {
data = inputFileHandler.getSlicelines<T>(size);
cout << "Size: " << size << '\n';
// Call the isolated sorting function
sortAndWrite(data, sorter, outputFileHandler, size);
}
}
}
/**
* Handles the command line arguments
* @param argc The number of arguments
* @param argv The arguments
* @return The data type to be used (integer or string)
*/
int handleCommandLineArguments(int argc, char *argv[]) {
int dataType;
int opt = getopt(argc, argv, "is");
if (opt == 'i') {
INPUT_FILE_PATH = "resource\\numbers.csv";
OUTPUT_FILE_PATH = "output\\times\\sorting_times_int.csv";
dataType = INTEGER_TYPE;
} else if (opt == 's') {
INPUT_FILE_PATH = "resource\\aurelio40000.txt";
OUTPUT_FILE_PATH = "output\\times\\sorting_times_string.csv";
dataType = STRING_TYPE;
} else {
throw std::invalid_argument("Usage: [-i] [-s]");
}
return dataType;
}
int main(int argc, char *argv[])
{
// Disable buffering on stdout and stderr, for use with the Python script
std::setbuf(stdout, NULL);
std::setbuf(stderr, NULL);
try
{
int dataType = handleCommandLineArguments(argc, argv);
FileHandler inputFileHandler(INPUT_FILE_PATH, true);
FileHandler outputFileHandler(OUTPUT_FILE_PATH, false);
cout << "Sorting Algorithm for " << (dataType == INTEGER_TYPE ? "integers" : "strings") << '\n';
if (dataType == INTEGER_TYPE)
runAllSortAlgorithms<int>(inputFileHandler, outputFileHandler);
else if (dataType == STRING_TYPE)
runAllSortAlgorithms<string>(inputFileHandler, outputFileHandler);
} catch (const std::exception& e) {
std::cerr << "Erro no programa principal: " << e.what() << '\n';
return 1;
}
cout << "Programa finalizado com sucesso!\n";
return 0;
}