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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
2
cpp/data_structures/heap.cpp
Normal file
2
cpp/data_structures/heap.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
namespace dlos {
|
||||
}
|
82
cpp/data_structures/heap.hpp
Normal file
82
cpp/data_structures/heap.hpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
// examples of how the heap can be used
|
||||
//
|
||||
// std::array<int, 10> a1 = {73, 6, 57, 88, 60, 42, 83, 72, 48, 85};
|
||||
// dlos::Heap<int, a1.size()> h = dlos::Heap<int, a1.size()>(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<int, 20> 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 <typename T, size_t N>
|
||||
class Heap {
|
||||
public:
|
||||
int size = 0;
|
||||
|
||||
private:
|
||||
std::array<T, N> &arr;
|
||||
|
||||
public:
|
||||
Heap(std::array<T, N> &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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
29
cpp/test.cpp
Normal file
29
cpp/test.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
#include "data_structures/heap.hpp"
|
||||
#include "algorithms/sort_heap.hpp"
|
||||
|
||||
int main() {
|
||||
std::array<int, 10> a1 = {73, 6, 57, 88, 60, 42, 83, 72, 48, 85};
|
||||
dlos::Heap<int, a1.size()> h = dlos::Heap<int, a1.size()>(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<int, 20> 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;
|
||||
}
|
Reference in New Issue
Block a user