🏗️」 wip(ex02): worked on the main, still need work on the algorithm code

This commit is contained in:
2025-08-30 14:03:42 +02:00
parent 3f17426cc8
commit c8a3c731c3
2 changed files with 11 additions and 13 deletions

View File

@@ -3,6 +3,7 @@
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <iomanip>
#include <sstream> #include <sstream>
#include <climits> #include <climits>
#include <cstdlib> #include <cstdlib>

View File

@@ -10,7 +10,8 @@ bool isPositiveInteger(const std::string &s) {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc < 2) { if (argc < 2)
{
std::cerr << "Error" << std::endl; std::cerr << "Error" << std::endl;
return 1; return 1;
} }
@@ -21,14 +22,16 @@ int main(int argc, char **argv)
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
std::string arg(argv[i]); std::string arg(argv[i]);
if (!isPositiveInteger(arg)) { if (!isPositiveInteger(arg))
{
std::cerr << "Error" << std::endl; std::cerr << "Error" << std::endl;
return 1; return 1;
} }
std::istringstream iss(arg); std::istringstream iss(arg);
long num; long num;
iss >> num; iss >> num;
if (num < 0 || num > INT_MAX) { if (num < 0 || num > INT_MAX)
{
std::cerr << "Error" << std::endl; std::cerr << "Error" << std::endl;
return 1; return 1;
} }
@@ -36,42 +39,36 @@ int main(int argc, char **argv)
deq.push_back(static_cast<int>(num)); deq.push_back(static_cast<int>(num));
} }
// Print before
std::cout << "Before: "; std::cout << "Before: ";
for (size_t i = 0; i < vec.size() && i < 6; i++) for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " "; std::cout << vec[i] << " ";
if (vec.size() > 6) if (vec.size() > 6)
std::cout << "[...]"; std::cout << "[...]";
std::cout << std::endl; std::cout << std::endl;
// Sort vector
clock_t startVec = clock(); clock_t startVec = clock();
vec = fordJohnsonSort(vec); vec = fordJohnsonSort(vec);
clock_t endVec = clock(); clock_t endVec = clock();
// Sort deque
clock_t startDeq = clock(); clock_t startDeq = clock();
deq = fordJohnsonSort(deq); deq = fordJohnsonSort(deq);
clock_t endDeq = clock(); clock_t endDeq = clock();
// Print after
std::cout << "After: "; std::cout << "After: ";
for (size_t i = 0; i < vec.size() && i < 6; i++) for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " "; std::cout << vec[i] << " ";
if (vec.size() > 6) if (vec.size() > 6)
std::cout << "[...]"; std::cout << "[...]";
std::cout << std::endl; std::cout << std::endl;
double vecTime = (double)(endVec - startVec) / CLOCKS_PER_SEC * 1e6; double vecTime = (double)(endVec - startVec) / CLOCKS_PER_SEC * 1000000.0;
double deqTime = (double)(endDeq - startDeq) / CLOCKS_PER_SEC * 1e6; double deqTime = (double)(endDeq - startDeq) / CLOCKS_PER_SEC * 1000000.0;
std::cout << "Time to process a range of " << vec.size() std::cout << "Time to process a range of " << vec.size()
<< " elements with std::vector : " << vecTime << " us" << std::endl; << " elements with std::vector : " << std::fixed << std::setprecision(1) << vecTime << " us" << std::endl;
std::cout << "Time to process a range of " << deq.size() std::cout << "Time to process a range of " << deq.size()
<< " elements with std::deque : " << deqTime << " us" << std::endl; << " elements with std::deque : " << std::fixed << std::setprecision(1) << deqTime << " us" << std::endl;
return 0; return 0;
} }