Compare commits

...

2 Commits

3 changed files with 243 additions and 0 deletions

58
ex02/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/30 13:38:12 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CXX = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = PmergeMe
SRCS = main.cpp #PmergeMe.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CXX) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CXX) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

111
ex02/PmergeMe.hpp Normal file
View File

@@ -0,0 +1,111 @@
#pragma once
#include <stdexcept>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <climits>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <deque>
std::vector<size_t> generateJacobsthal(size_t n)
{
std::vector<size_t> seq;
if (n == 0)
return seq;
seq.push_back(0);
if (n == 1)
return seq;
seq.push_back(1);
size_t j0 = 0, j1 = 1;
while (true)
{
size_t jn = j1 + 2 * j0;
if (jn >= n)
break;
seq.push_back(jn);
j0 = j1;
j1 = jn;
}
return seq;
}
template <typename T>
struct Pair {
T first;
T second;
};
template <typename T>
struct PairComp {
bool operator()(const Pair<T> &x, const Pair<T> &y) const
{
return x.second < y.second;
}
};
template <typename Container>
Container fordJohnsonSort(const Container &input)
{
typedef typename Container::value_type T;
Container result;
if (input.size() <= 1)
return input;
std::vector<Pair<T> > pairs;
size_t i = 0;
for (; i + 1 < input.size(); i += 2)
{
T a = input[i];
T b = input[i+1];
if (a > b)
std::swap(a, b);
Pair<T> p;
p.first = a;
p.second = b;
pairs.push_back(p);
}
std::sort(pairs.begin(), pairs.end(), PairComp<T>());
for (size_t k = 0; k < pairs.size(); ++k)
result.push_back(pairs[k].second);
std::vector<size_t> order = generateJacobsthal(pairs.size());
std::vector<bool> inserted(pairs.size(), false);
for (size_t idx = 0; idx < order.size(); ++idx)
{
size_t j = order[idx];
if (j < pairs.size() && !inserted[j])
{
typename Container::iterator pos = std::lower_bound(
result.begin(), result.end(), pairs[j].first);
result.insert(pos, pairs[j].first);
inserted[j] = true;
}
}
for (size_t j = 0; j < pairs.size(); ++j)
{
if (!inserted[j])
{
typename Container::iterator pos = std::lower_bound(
result.begin(), result.end(), pairs[j].first);
result.insert(pos, pairs[j].first);
}
}
if (i < input.size())
{
T leftover = input[i];
typename Container::iterator pos = std::lower_bound(result.begin(), result.end(), leftover);
result.insert(pos, leftover);
}
return result;
}

74
ex02/main.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include "PmergeMe.hpp"
bool isPositiveInteger(const std::string &s) {
if (s.empty()) return false;
for (size_t i = 0; i < s.size(); i++) {
if (!isdigit(s[i])) return false;
}
return true;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cerr << "Error" << std::endl;
return 1;
}
std::vector<int> vec;
std::deque<int> deq;
for (int i = 1; i < argc; ++i)
{
std::string arg(argv[i]);
if (!isPositiveInteger(arg))
{
std::cerr << "Error" << std::endl;
return 1;
}
std::istringstream iss(arg);
long num;
iss >> num;
if (num < 0 || num > INT_MAX)
{
std::cerr << "Error" << std::endl;
return 1;
}
vec.push_back(static_cast<int>(num));
deq.push_back(static_cast<int>(num));
}
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;
clock_t startVec = clock();
vec = fordJohnsonSort(vec);
clock_t endVec = clock();
clock_t startDeq = clock();
deq = fordJohnsonSort(deq);
clock_t endDeq = clock();
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 * 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 : " << std::fixed << std::setprecision(1) << vecTime << " us" << std::endl;
std::cout << "Time to process a range of " << deq.size()
<< " elements with std::deque : " << std::fixed << std::setprecision(1) << deqTime << " us" << std::endl;
return 0;
}