diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..c11e6fa --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,26 @@ +CXX := c++ +CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g +SRCDIR := src +OBJDIR := obj +NAME := PmergeMe + +SRCS := $(wildcard $(SRCDIR)/*.cpp) +OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + mkdir -p obj + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(NAME): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR) + +fclean: clean + rm -fr $(NAME) + +re: fclean + @make --no-print-directory all diff --git a/ex02/src/PmergeMe.cpp b/ex02/src/PmergeMe.cpp new file mode 100644 index 0000000..84b8f69 --- /dev/null +++ b/ex02/src/PmergeMe.cpp @@ -0,0 +1,33 @@ +#include "./PmergeMe.hpp" +#include +#include + +void sort_pair(std::pair* pairs, size_t len) +{ + for (size_t i = 0; i < len; i++) + { + if (pairs[i].second > pairs[i].first) + std::swap(pairs[i].first, pairs[i].second); + } +} + +void sort_pairs(std::pair* pairs, size_t len) +{ + if (len == 1) + return; + + size_t count = 0; + for (size_t i = 0; i < len; i++) + { + if (pairs[i].first > pairs[i + 1].first) + { + std::swap(pairs[i], pairs[i + 1]); + count++; + } + } + + if (count == 0) + return; + + sort_pair(pairs, len - 1); +} \ No newline at end of file diff --git a/ex02/src/PmergeMe.hpp b/ex02/src/PmergeMe.hpp new file mode 100644 index 0000000..5b367e1 --- /dev/null +++ b/ex02/src/PmergeMe.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include +#include +#include + +void sort_pair(std::pair* pairs, size_t len); +void sort_pairs(std::pair* pairs, size_t len); + +template +std::pair* create_pairs(const T& array, size_t len) +{ + std::pair* pairs = new std::pair[len]; + + for (size_t i = 0; i != len; i++) + { + if (i % 2) + pairs[i].second = array[i]; + else + pairs[i].first = array[i]; + } + if (len > 0 && len % 2) + pairs[len - 1].second = pairs[len - 1].first; + return pairs; +} + +template +void binary_search(std::pair* pairs, size_t len, T& array) +{ + (void) pairs; + (void) array; + std::vector bozo; + for (size_t i = 0; i < len; i++) + { + + } +} + +template +void PmergeMe(T& array) +{ + std::size_t len = array.size() / 2 + array.size() % 2; + std::pair* pairs = create_pairs(array, len); + sort_pair(pairs, len); + sort_pairs(pairs, len); + std::cout << pairs; + array.clear(); + binary_search(pairs, len, array); +} \ No newline at end of file diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp new file mode 100644 index 0000000..3661e80 --- /dev/null +++ b/ex02/src/main.cpp @@ -0,0 +1,17 @@ +#include "PmergeMe.hpp" +#include +#include +#include + +int main(int ac, char** av) +{ + std::vector input(ac); + std::cout << "Before: "; + for (int i = 1; i != ac; i++) + { + input.push_back(atoi(av[i])); + std::cout << av[i] << " "; + } + std::cout << std::endl; + PmergeMe(input); +} \ No newline at end of file