From 50873ce45028eb266bef5d5e21ed8f0429a69d14 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 8 Oct 2025 12:12:00 -0500 Subject: [PATCH] Add initial cpp heapsort, needs refactor (likely use constexpr) --- cpp/algorithms/sort_heap.cpp | 2 + cpp/algorithms/sort_heap.hpp | 23 ++++++++++ cpp/data_structures/heap.cpp | 2 + cpp/data_structures/heap.hpp | 82 ++++++++++++++++++++++++++++++++++++ cpp/test.cpp | 29 +++++++++++++ 5 files changed, 138 insertions(+) create mode 100644 cpp/algorithms/sort_heap.cpp create mode 100644 cpp/algorithms/sort_heap.hpp create mode 100644 cpp/data_structures/heap.cpp create mode 100644 cpp/data_structures/heap.hpp create mode 100644 cpp/test.cpp diff --git a/cpp/algorithms/sort_heap.cpp b/cpp/algorithms/sort_heap.cpp new file mode 100644 index 0000000..ce1aa67 --- /dev/null +++ b/cpp/algorithms/sort_heap.cpp @@ -0,0 +1,2 @@ +namespace dlos { +} diff --git a/cpp/algorithms/sort_heap.hpp b/cpp/algorithms/sort_heap.hpp new file mode 100644 index 0000000..7ec70b2 --- /dev/null +++ b/cpp/algorithms/sort_heap.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "../data_structures/heap.hpp" + +namespace dlos { + + template + void heapsort(std::array &a) { + dlos::Heap heap = dlos::Heap(a); + heap.maxHeap(); + heapsort(heap); + } + + template + void heapsort(Heap &heap) { + for (int i = heap.size - 1; i > 0; i--) { + heap.nodeSwap(0, i); + heap.size -= 1; + heap.siftDown(0); + } + } + +} diff --git a/cpp/data_structures/heap.cpp b/cpp/data_structures/heap.cpp new file mode 100644 index 0000000..ce1aa67 --- /dev/null +++ b/cpp/data_structures/heap.cpp @@ -0,0 +1,2 @@ +namespace dlos { +} diff --git a/cpp/data_structures/heap.hpp b/cpp/data_structures/heap.hpp new file mode 100644 index 0000000..b1e1d5a --- /dev/null +++ b/cpp/data_structures/heap.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include + +// examples of how the heap can be used +// +// std::array a1 = {73, 6, 57, 88, 60, 42, 83, 72, 48, 85}; +// dlos::Heap h = dlos::Heap(a1); +// h.maxHeap(); +// dlos::heapsort(h); +// for (int i = 0; i < a1.size(); i++) { +// std::cout << h.getValueAt(i) << ", "; +// if (i == a1.size() - 1) std::cout << std::endl; +// } +// +// std::array a2 = {98, 8, 108, 91, 139, 49, 181, 212, 77, 85, 73, 6, 57, 88, 60, 42, 83, 72, 48, 85}; +// dlos::heapsort(a2); +// for (int i = 0; i < a2.size(); i++) { +// std::cout << a2[i] << ", "; +// if (i == a2.size() - 1) std::cout << std::endl; +// } + +namespace dlos { + + template + class Heap { + public: + int size = 0; + + private: + std::array &arr; + + public: + Heap(std::array &arr) : arr(arr) { size = N; } + + T getValueAt(int i) const { return arr[i]; } + + void nodeSwap(int a, int b) { + T temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; + } + + void siftDown(int i) { + int target = indexOfLargerChild(i); + while (target > 0 && arr[i] < arr[target]) { + nodeSwap(i, target); + i = target; + target = indexOfLargerChild(i); + }; + } + + void maxHeap() { + for (int i = parent(size - 1); i >= 0; i--) { + siftDown(i); + } + } + + private: + int parent(int i) { return (i > 0) ? (i - 1) / 2 : -1; } + + int leftChild(int i) { return i * 2 + 1; } + + int rightChild(int i) { return i * 2 + 2; } + + int leftSibling(int i) { return (i % 2 == 0) ? i - 1 : -1; } + + int rightSibling(int i) { return (i % 2 == 1) ? i + 1 : -1; } + + int indexOfLargerChild(int i) { + int larger = leftChild(i); + if (larger >= size) { + return -1; + } + if (larger + 1 < size && arr[larger] < arr[larger + 1]) { + larger = larger + 1; + } + return larger; + } + }; + +} diff --git a/cpp/test.cpp b/cpp/test.cpp new file mode 100644 index 0000000..727bd99 --- /dev/null +++ b/cpp/test.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include "data_structures/heap.hpp" +#include "algorithms/sort_heap.hpp" + +int main() { + std::array a1 = {73, 6, 57, 88, 60, 42, 83, 72, 48, 85}; + dlos::Heap h = dlos::Heap(a1); + h.maxHeap(); + dlos::heapsort(h); + + for (int i = 0; i < a1.size(); i++) { + if (i == 0) std::cout << "sorted: "; + std::cout << h.getValueAt(i) << ", "; + if (i == a1.size() - 1) std::cout << std::endl; + } + + std::array a2 = {98, 8, 108, 91, 139, 49, 181, 212, 77, 85, 73, 6, 57, 88, 60, 42, 83, 72, 48, 85}; + dlos::heapsort(a2); + + for (int i = 0; i < a2.size(); i++) { + if (i == 0) std::cout << "sorted: "; + std::cout << a2[i] << ", "; + if (i == a2.size() - 1) std::cout << std::endl; + } + + return 0; +}