42#include <gtest/gtest.h>
47using namespace testing;
54 std::vector<int> tmp(v.size() + 1);
55 for (
size_t i = 0; i < v.size(); ++i)
62TEST(ArrayHeapAlgos, ValidHeapDetectsHeapAndNonHeap)
65 std::vector<int> heap_like{1, 3, 2, 7, 5, 4};
70 std::vector<int> not_heap{3, 1, 2};
75TEST(ArrayHeapAlgos, SiftUpRestoresHeapAfterInsertAtEnd)
78 int arr[5] = {0, 1, 3, 2, 0};
90TEST(ArrayHeapAlgos, SiftDownRestoresHeapAfterRootViolation)
93 int arr[6] = {0, 9, 1, 2, 3, 4};
102TEST(ArrayHeapAlgos, SiftDownUpRestoresHeapAfterInternalUpdate)
105 int arr[8] = {0, 1, 3, 2, 7, 5, 4, 8};
113 EXPECT_EQ(arr[1], 0);
121TEST(ArrayHeapAlgos, HeapsortMatchesStdSort)
123 std::vector<int> v{5, 1, 4, 2, 8, 0, 3, 7, 6, 9};
131TEST(ArrayHeapAlgos, FasterHeapsortMatchesStdSort)
133 std::vector<int> v{12, -1, 5, 5, 3, 99, 0, -10, 7, 2, 4};
static bool is_min_heap(const std::vector< int > &v)
Minimal std::expected-style result type for C++20.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
void sift_down_up(T *ptr, const size_t l, const size_t i, const size_t r, Compare &cmp)
Restore the heap property by sifting down and then sifting up.
void sift_down(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position l downwards.
bool valid_heap(T *array, const size_t l, const size_t r, const Compare &cmp=Compare())
Check whether a range satisfies the heap property.
constexpr void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
constexpr void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
T & sift_up(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position r upwards.
Fixed-capacity binary heap and heapsort algorithms.