From 80c051b44a04240ca6d61e98e9af2293c4370db1 Mon Sep 17 00:00:00 2001 From: Ikatono Date: Fri, 4 Oct 2024 16:11:55 -0500 Subject: [PATCH] initial commit --- graph/breadth.cpp | 0 graph/breadth.hpp | 16 +++++ sorting/heapsort.cpp | 145 ++++++++++++++++++++++++++++++++++++++++++ sorting/heapsort.hpp | 13 ++++ sorting/quicksort.cpp | 76 ++++++++++++++++++++++ sorting/quicksort.hpp | 13 ++++ utils.hpp | 25 ++++++++ 7 files changed, 288 insertions(+) create mode 100644 graph/breadth.cpp create mode 100644 graph/breadth.hpp create mode 100644 sorting/heapsort.cpp create mode 100644 sorting/heapsort.hpp create mode 100644 sorting/quicksort.cpp create mode 100644 sorting/quicksort.hpp create mode 100644 utils.hpp diff --git a/graph/breadth.cpp b/graph/breadth.cpp new file mode 100644 index 0000000..e69de29 diff --git a/graph/breadth.hpp b/graph/breadth.hpp new file mode 100644 index 0000000..f79d8e6 --- /dev/null +++ b/graph/breadth.hpp @@ -0,0 +1,16 @@ +#ifndef H_DD59527C0C7B481DBB78E4BCF068E1CA +#define H_DD59527C0C7B481DBB78E4BCF068E1CA + +#include +#include + +template +struct GraphPath +{ + std::vector path; +}; + +template +GraphPath breadthSearch(const NodeIterable &nodes, const EdgeIterable &edges); + +#endif //H_DD59527C0C7B481DBB78E4BCF068E1CA diff --git a/sorting/heapsort.cpp b/sorting/heapsort.cpp new file mode 100644 index 0000000..d73bd37 --- /dev/null +++ b/sorting/heapsort.cpp @@ -0,0 +1,145 @@ +#include "heapsort.hpp" +#include "utils.hpp" +#include +#include +#include + +template +class HeapSorter +{ + public: + using pointed = decltype(*std::declval()); + using difftype = decltype(std::declval() - std::declval()); + HeapSorter(T _begin, T _end, const std::function &_comparer) + : begin(_begin), end(_end), comp(_comparer), len(_end - _begin) + { + + } + void doSort() + { + buildMaxHeap(); + // std::cout << "after build heap" << std::endl; + // arrayprint(begin, end); + // std::cout << std::endl; + for (difftype i = len - 1; i >= 1; i--) + { + const auto tmp = *begin; + *begin = *(begin + i); + *(begin + i) = tmp; + maxHeapify(0, i); + } + } + private: + const T begin; + const T end; + const std::function ∁ + const difftype len; + void maxHeapify(difftype start, difftype bottom) + { + const auto _bottom = begin + bottom; + auto current = begin + start; + while (current < _bottom) + { + const auto diff = current - begin; + const auto left = (diff * 2) + 1; + const auto right = left + 1; + if (left < bottom) + { + const auto _left = begin + left; + if (comp(*current, *_left)) + { + if (right < bottom && comp(*_left, *(begin + right))) + { + const auto _right = begin+right; + assert(_right != _bottom); + const auto tmp = *_right; + *_right = *current; + *current = tmp; + current = _right; + } + else + { + const auto tmp = *_left; + assert(_left != _bottom); + *_left = *current; + *current = tmp; + current = _left; + } + } + else + { + if (right < bottom && comp(*current, *(begin+right))) + { + const auto _right= begin+right; + assert(_right != _bottom); + const auto tmp = *_right; + *_right = *current; + *current = tmp; + current = _right; + } + else + { + break; + } + } + } + else + { + break; + } + } + } + void buildMaxHeap() + { + difftype i = len / 2 + 1; + while (true) + { + maxHeapify(i, len); + if (i == 0) + break; + i--; + } + } + +}; + +template +void heapsort(T begin, T end, std::function()), decltype(*std::declval()))> comparer) +{ + HeapSorter sorter(begin, end, comparer); + sorter.doSort(); +} + +template +void heapsort(T begin, T end) +{ + heapsort(begin, end, [](decltype(*std::declval()) f, decltype(*std::declval()) s){ return f < s; }); +} + +int main() +{ + //last value is to find OOB + std::vector vec{12345, 1,5,3,2,5,7,9,-3,12,0, 9999}; + arrayprint(vec.begin()+1, vec.end()-1); + std::cout << std::endl; + heapsort(vec.begin()+1, vec.end()-1); + arrayprint(vec.begin()+1, vec.end()-1); + std::cout << std::endl; + std::cout << std::endl; + + std::cout << "sort by reverse iterators" << std::endl; + arrayprint(vec.begin()+1, vec.end()-1); + std::cout << std::endl; + heapsort(vec.rbegin()+1, vec.rend()-1); + arrayprint(vec.begin()+1, vec.end()-1); + std::cout << std::endl; + std::cout << std::endl; + + std::vector vec2; + arrayprint(vec2.begin(), vec2.end()); + std::cout << std::endl; + heapsort(vec2.begin(), vec2.end()); + arrayprint(vec2.begin(), vec2.end()); + std::cout << std::endl; + return 0; +} diff --git a/sorting/heapsort.hpp b/sorting/heapsort.hpp new file mode 100644 index 0000000..e6c15d2 --- /dev/null +++ b/sorting/heapsort.hpp @@ -0,0 +1,13 @@ +#ifndef H_9E0FB1CD4A804878B163933022B6AE52 +#define H_9E0FB1CD4A804878B163933022B6AE52 + +#include +#include + +template +void heapsort(T begin, T end); + +template +void heapsort(T begin, T end, std::function()), decltype(*std::declval()))> comparer); + +#endif //H_9E0FB1CD4A804878B163933022B6AE52 \ No newline at end of file diff --git a/sorting/quicksort.cpp b/sorting/quicksort.cpp new file mode 100644 index 0000000..cce5f5a --- /dev/null +++ b/sorting/quicksort.cpp @@ -0,0 +1,76 @@ +#include "quicksort.hpp" +#include "utils.hpp" +#include +#include +#include +#include + +namespace +{ + +template +T quicksort_partition(T begin, T end, const std::function()), decltype(*std::declval()))> comparer) +{ + std::cout << "part: " << *begin << std::endl; + assert(begin != end); + // using difftype = decltype(std::declval() - std::declval()); + const T piv = std::next(end, -1); + T swapIfLesser = begin; + while (swapIfLesser != piv && comparer(*swapIfLesser, *piv)) + { + swapIfLesser = std::next(swapIfLesser); + } + swapIfLesser = std::next(swapIfLesser); + T active = std::next(swapIfLesser); + while (active != piv) + { + if (comparer(*active, *piv)) + { + std::iter_swap(swapIfLesser, active); + swapIfLesser = std::next(swapIfLesser); + } + active = std::next(active); + } + const T pivFinal = std::next(swapIfLesser); + std::iter_swap(pivFinal, piv); + return pivFinal; +} + +} + +template +void quicksort(T begin, T end, std::function()), decltype(*std::declval()))> comparer) +{ + if (begin != end) + { + auto pivot = quicksort_partition(begin, end, comparer); + quicksort(begin, pivot); + quicksort(std::next(pivot), end); + } +} + +template +void quicksort(T begin, T end) +{ + const auto comparer = [](decltype(*std::declval()) f, decltype(*std::declval()) s){ return f < s; }; + quicksort(begin, end, comparer); +} + +int main() +{ + //last value is to find OOB + std::vector vec{12345, 1,5,3,2,5,7,9,-3,12,0, 9999}; + arrayprint(vec.begin()+1, vec.end()-1); + std::cout << std::endl; + quicksort(vec.begin()+1, vec.end()-1); + arrayprint(vec.begin()+1, vec.end()-1); + std::cout << std::endl; + std::cout << std::endl; + std::vector vec2; + arrayprint(vec2.begin(), vec2.end()); + std::cout << std::endl; + quicksort(vec2.begin(), vec2.end()); + arrayprint(vec2.begin(), vec2.end()); + std::cout << std::endl; + return 0; +} \ No newline at end of file diff --git a/sorting/quicksort.hpp b/sorting/quicksort.hpp new file mode 100644 index 0000000..bf3f42e --- /dev/null +++ b/sorting/quicksort.hpp @@ -0,0 +1,13 @@ +#ifndef H_DA7CBF247B3E4DFDB1F13657E787EBE4 +#define H_DA7CBF247B3E4DFDB1F13657E787EBE4 + +#include +#include + +template +void quicksort(T begin, T end); + +template +void quicksort(T begin, T end, std::function()), decltype(*std::declval()))> comparer); + +#endif //H_DA7CBF247B3E4DFDB1F13657E787EBE4 \ No newline at end of file diff --git a/utils.hpp b/utils.hpp new file mode 100644 index 0000000..6883780 --- /dev/null +++ b/utils.hpp @@ -0,0 +1,25 @@ +#ifndef H_A8A1FCC0D9024403B10B2B58FDE2112E +#define H_A8A1FCC0D9024403B10B2B58FDE2112E + +#include +#include + +//TODO fix trailing comma +template +void arrayprint(T begin, T end) +{ + std::cout << '['; + auto it = begin; + auto next = std::next(it, 1); + while (it != end) + { + std::cout << *it; + if (next != end) + std::cout << ','; + it = next; + next = std::next(it, 1); + } + std::cout << ']'; +} + +#endif //H_A8A1FCC0D9024403B10B2B58FDE2112E