diff --git a/ex02/src/PmergeMe.cpp b/ex02/src/PmergeMe.cpp index 84b8f69..d04175e 100644 --- a/ex02/src/PmergeMe.cpp +++ b/ex02/src/PmergeMe.cpp @@ -1,6 +1,4 @@ #include "./PmergeMe.hpp" -#include -#include void sort_pair(std::pair* pairs, size_t len) { @@ -17,7 +15,7 @@ void sort_pairs(std::pair* pairs, size_t len) return; size_t count = 0; - for (size_t i = 0; i < len; i++) + for (size_t i = 0; i < len - 1; i++) { if (pairs[i].first > pairs[i + 1].first) { @@ -29,5 +27,5 @@ void sort_pairs(std::pair* pairs, size_t len) if (count == 0) return; - sort_pair(pairs, len - 1); + sort_pairs(pairs, len - 1); } \ No newline at end of file diff --git a/ex02/src/PmergeMe.hpp b/ex02/src/PmergeMe.hpp index 5b367e1..5575380 100644 --- a/ex02/src/PmergeMe.hpp +++ b/ex02/src/PmergeMe.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include @@ -16,13 +15,11 @@ std::pair* create_pairs(const T& array, size_t len) for (size_t i = 0; i != len; i++) { - if (i % 2) - pairs[i].second = array[i]; - else - pairs[i].first = array[i]; + pairs[i].second = array[i * 2]; + pairs[i].first = array[i * 2 + 1]; } - if (len > 0 && len % 2) - pairs[len - 1].second = pairs[len - 1].first; + if (len % 2) + pairs[len - 1].first = pairs[len - 1].second; return pairs; } diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 3661e80..be24b23 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -5,13 +5,20 @@ int main(int ac, char** av) { - std::vector input(ac); + std::vector input(ac - 1); std::cout << "Before: "; for (int i = 1; i != ac; i++) { - input.push_back(atoi(av[i])); + input[i - 1] = atoi(av[i]); std::cout << av[i] << " "; } std::cout << std::endl; PmergeMe(input); + std::cout << "After: "; + for (int i = 1; i != ac; i++) + { + input[i - 1] = atoi(av[i]); + std::cout << av[i] << " "; + } + std::cout << std::endl; } \ No newline at end of file