initial commit
This commit is contained in:
0
graph/breadth.cpp
Normal file
0
graph/breadth.cpp
Normal file
16
graph/breadth.hpp
Normal file
16
graph/breadth.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef H_DD59527C0C7B481DBB78E4BCF068E1CA
|
||||
#define H_DD59527C0C7B481DBB78E4BCF068E1CA
|
||||
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
template <typename NodeType>
|
||||
struct GraphPath
|
||||
{
|
||||
std::vector<NodeType> path;
|
||||
};
|
||||
|
||||
template <typename NodeType>
|
||||
GraphPath<NodeType> breadthSearch(const NodeIterable &nodes, const EdgeIterable &edges);
|
||||
|
||||
#endif //H_DD59527C0C7B481DBB78E4BCF068E1CA
|
||||
145
sorting/heapsort.cpp
Normal file
145
sorting/heapsort.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "heapsort.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
class HeapSorter
|
||||
{
|
||||
public:
|
||||
using pointed = decltype(*std::declval<T>());
|
||||
using difftype = decltype(std::declval<T>() - std::declval<T>());
|
||||
HeapSorter(T _begin, T _end, const std::function<bool(pointed, pointed)> &_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<bool(pointed, pointed)> ∁
|
||||
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 <std::random_access_iterator T>
|
||||
void heapsort(T begin, T end, std::function<bool(decltype(*std::declval<T>()), decltype(*std::declval<T>()))> comparer)
|
||||
{
|
||||
HeapSorter sorter(begin, end, comparer);
|
||||
sorter.doSort();
|
||||
}
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
void heapsort(T begin, T end)
|
||||
{
|
||||
heapsort(begin, end, [](decltype(*std::declval<T>()) f, decltype(*std::declval<T>()) s){ return f < s; });
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//last value is to find OOB
|
||||
std::vector<int32_t> 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<int32_t> 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;
|
||||
}
|
||||
13
sorting/heapsort.hpp
Normal file
13
sorting/heapsort.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef H_9E0FB1CD4A804878B163933022B6AE52
|
||||
#define H_9E0FB1CD4A804878B163933022B6AE52
|
||||
|
||||
#include <iterator>
|
||||
#include <functional>
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
void heapsort(T begin, T end);
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
void heapsort(T begin, T end, std::function<bool(decltype(*std::declval<T>()), decltype(*std::declval<T>()))> comparer);
|
||||
|
||||
#endif //H_9E0FB1CD4A804878B163933022B6AE52
|
||||
76
sorting/quicksort.cpp
Normal file
76
sorting/quicksort.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "quicksort.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
T quicksort_partition(T begin, T end, const std::function<bool(decltype(*std::declval<T>()), decltype(*std::declval<T>()))> comparer)
|
||||
{
|
||||
std::cout << "part: " << *begin << std::endl;
|
||||
assert(begin != end);
|
||||
// using difftype = decltype(std::declval<T>() - std::declval<T>());
|
||||
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 <std::random_access_iterator T>
|
||||
void quicksort(T begin, T end, std::function<bool(decltype(*std::declval<T>()), decltype(*std::declval<T>()))> comparer)
|
||||
{
|
||||
if (begin != end)
|
||||
{
|
||||
auto pivot = quicksort_partition(begin, end, comparer);
|
||||
quicksort(begin, pivot);
|
||||
quicksort(std::next(pivot), end);
|
||||
}
|
||||
}
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
void quicksort(T begin, T end)
|
||||
{
|
||||
const auto comparer = [](decltype(*std::declval<T>()) f, decltype(*std::declval<T>()) s){ return f < s; };
|
||||
quicksort(begin, end, comparer);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//last value is to find OOB
|
||||
std::vector<int32_t> 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<int32_t> 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;
|
||||
}
|
||||
13
sorting/quicksort.hpp
Normal file
13
sorting/quicksort.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef H_DA7CBF247B3E4DFDB1F13657E787EBE4
|
||||
#define H_DA7CBF247B3E4DFDB1F13657E787EBE4
|
||||
|
||||
#include <iterator>
|
||||
#include <functional>
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
void quicksort(T begin, T end);
|
||||
|
||||
template <std::random_access_iterator T>
|
||||
void quicksort(T begin, T end, std::function<bool(decltype(*std::declval<T>()), decltype(*std::declval<T>()))> comparer);
|
||||
|
||||
#endif //H_DA7CBF247B3E4DFDB1F13657E787EBE4
|
||||
25
utils.hpp
Normal file
25
utils.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef H_A8A1FCC0D9024403B10B2B58FDE2112E
|
||||
#define H_A8A1FCC0D9024403B10B2B58FDE2112E
|
||||
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
|
||||
//TODO fix trailing comma
|
||||
template <typename T>
|
||||
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
|
||||
Reference in New Issue
Block a user