diff --git a/algorithms/C++/merge_sort/msort.cpp b/algorithms/C++/merge_sort/msort.cpp new file mode 100644 index 0000000..5c9fabe --- /dev/null +++ b/algorithms/C++/merge_sort/msort.cpp @@ -0,0 +1,95 @@ +#include "msort.hpp" + +using vecIter = std::vector::iterator; + +void merge(vecIter beginFirst, + vecIter endFirst, + vecIter beginSecond, + vecIter endSecond) { + auto const begin = beginFirst; + + std::vector temp(std::distance(beginFirst, endSecond)); + auto it = temp.begin(); + + while (beginFirst != endFirst && beginSecond != endSecond) { + if (*beginFirst <= *beginSecond) { + *it = *beginFirst; + beginFirst++; + } else { + *it = *beginSecond; + beginSecond++; + } + + it++; + } + + while (beginFirst != endFirst) { + *it = *beginFirst; + it++; + beginFirst++; + } + + while (beginSecond != endSecond) { + *it = *beginSecond; + it++; + beginSecond++; + } + + it = temp.begin(); + beginFirst = begin; + for (; beginFirst != endSecond; beginFirst++, it++) { + *beginFirst = *it; + } +} + +void msort(vecIter begin, vecIter end) { + size_t len = std::distance(begin, end); + if (len <= 1) { + return; + } + + auto mid = begin + (len / 2); + + msort(begin, mid); + msort(mid, end); + + merge(begin, mid, mid, end); +} + +/* +bool isSorted(vecIter begin, vecIter end) { + for (auto hare = begin + 1; hare != end; hare++, begin++) { + if (*hare < *begin) { + return false; + } + } + + return true; +} + + +int main() { + std::vector vec(100000); + + // First create an instance of an engine. + std::random_device rnd_device; + // Specify the engine and distribution. + std::mt19937 mersenne_engine{rnd_device()}; // Generates random integers + std::uniform_int_distribution dist{1, 1024}; + + auto gen = [&dist, &mersenne_engine]() { + return dist(mersenne_engine); + }; + + std::generate(std::begin(vec), std::end(vec), gen); + assert(isSorted(vec.begin(), vec.end()) == false); + + msort(vec.begin(), vec.end()); + + assert(isSorted(vec.begin(), vec.end())); + + std::cout << "Done"; + + return 0; +} +*/ diff --git a/algorithms/C++/merge_sort/msort.hpp b/algorithms/C++/merge_sort/msort.hpp new file mode 100644 index 0000000..24731ce --- /dev/null +++ b/algorithms/C++/merge_sort/msort.hpp @@ -0,0 +1,20 @@ +#ifndef M_SORT_H +#define M_SORT_H + +//#include + +#include +//#include +#include +//#include +#include + +/** + * @brief Performs merge sort on a range defined by two iterators. + * + * @param begin The start of the range. + * @param end The end of the range. + */ +void msort(std::vector::iterator begin, std::vector::iterator end); + +#endif