🏗️」 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 <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <climits>
#include <cstdlib>

View File

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