Add initial cpp heapsort, needs refactor (likely use constexpr)
This commit is contained in:
2
cpp/algorithms/sort_heap.cpp
Normal file
2
cpp/algorithms/sort_heap.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
namespace dlos {
|
||||
}
|
||||
23
cpp/algorithms/sort_heap.hpp
Normal file
23
cpp/algorithms/sort_heap.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "../data_structures/heap.hpp"
|
||||
|
||||
namespace dlos {
|
||||
|
||||
template<typename T, size_t N>
|
||||
void heapsort(std::array<T, N> &a) {
|
||||
dlos::Heap<T, N> heap = dlos::Heap<T, N>(a);
|
||||
heap.maxHeap();
|
||||
heapsort(heap);
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
void heapsort(Heap<T, N> &heap) {
|
||||
for (int i = heap.size - 1; i > 0; i--) {
|
||||
heap.nodeSwap(0, i);
|
||||
heap.size -= 1;
|
||||
heap.siftDown(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user