From ef5f952519ce1a32df1911b83e3c5d68c548b37a Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 25 May 2021 18:40:40 -0400 Subject: [PATCH] Add example for heap sort --- cpp/algorithms/sorting/CMakeLists.txt | 1 + cpp/algorithms/sorting/heap/CMakeLists.txt | 17 +++++ cpp/algorithms/sorting/heap/heap-sort.cpp | 39 ++++++++++++ cpp/algorithms/sorting/heap/lib-heap.cpp | 72 ++++++++++++++++++++++ cpp/algorithms/sorting/heap/lib-heap.hpp | 27 ++++++++ 5 files changed, 156 insertions(+) create mode 100644 cpp/algorithms/sorting/heap/CMakeLists.txt create mode 100644 cpp/algorithms/sorting/heap/heap-sort.cpp create mode 100644 cpp/algorithms/sorting/heap/lib-heap.cpp create mode 100644 cpp/algorithms/sorting/heap/lib-heap.hpp diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index 4c85cf2..fae1404 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(merge) add_subdirectory(selection) add_subdirectory(insertion) add_subdirectory(bubble) +add_subdirectory(heap) diff --git a/cpp/algorithms/sorting/heap/CMakeLists.txt b/cpp/algorithms/sorting/heap/CMakeLists.txt new file mode 100644 index 0000000..38ef02a --- /dev/null +++ b/cpp/algorithms/sorting/heap/CMakeLists.txt @@ -0,0 +1,17 @@ +############################################################################### +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: A basic CMakeLists configuration to practice heap sort ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### + +cmake_minimum_required(VERSION 3.16) +project(HeapSort LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(heap-sort "heap-sort.cpp") + +add_library(lib-heap "lib-heap.cpp") +target_link_libraries(heap-sort lib-heap) diff --git a/cpp/algorithms/sorting/heap/heap-sort.cpp b/cpp/algorithms/sorting/heap/heap-sort.cpp new file mode 100644 index 0000000..b14f77f --- /dev/null +++ b/cpp/algorithms/sorting/heap/heap-sort.cpp @@ -0,0 +1,39 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of heap sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-heap.hpp" + +#include +#include +#include +#include + +int main(const int argc, const char * argv[]) +{ + srand(time(nullptr)); + std::vector array(ARRAY_LENGTH); +// array[0] = 0; // Don't use the 0 index + std::generate(array.begin(), array.end(), [](){ return rand() % 1000;}); + + auto print = [](std::vector array) { + for (const auto &i : array) { + std::cout << i << ", "; + } + std::cout << std::endl; + }; + + std::cout << "Unsorted array: \n"; + print(array); + + // Builds a max heap from the array, and then sorts using HeapSort + HeapSort(array); + + std::cout << "Sorted array: \n"; + print(array); +} diff --git a/cpp/algorithms/sorting/heap/lib-heap.cpp b/cpp/algorithms/sorting/heap/lib-heap.cpp new file mode 100644 index 0000000..96e8834 --- /dev/null +++ b/cpp/algorithms/sorting/heap/lib-heap.cpp @@ -0,0 +1,72 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of heap sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-heap.hpp" + +#include +#include + +// Parent is unused, but included for completeness +size_t Parent(const size_t &index) { return index / 2;} +size_t Left(const size_t &index) { return 2 * index + 1;} +size_t Right(const size_t &index) { return (2 * index) + 2;} + +void MaxHeapify(std::vector &array, size_t thisIndex, int &heapSize) +{ + // Get an index for the left and right nodes attached to thisIndex + size_t l = Left(thisIndex); + size_t r = Right(thisIndex); + // Assume thisIndex holds the largest value until proven otherwise + size_t largest = thisIndex; + // For both conditions, make sure l and r are within the heaps size + // If either index l or r hold values that are larger than that of thisIndex + // + Update the largest index + if (l < heapSize && array[l] > array[thisIndex]) { + largest = l; + } + if (r < heapSize && array[r] > array[largest]) { + largest = r; + } + // If l or r contain any value larger than that of thisIndex + if (largest != thisIndex) { + // Swap their values + std::swap(array[thisIndex], array[largest]); + // Run MaxHeapify on the array, using the largest index as thisIndex + MaxHeapify(array, largest, heapSize); + } +} + +void BuildMaxHeap(std::vector &array, int &heapSize) +{ + // For each value within the heap, starting at last index moving to the first + for (int i = (array.size() / 2); i >= 0; i--) { + // Call MaxHeapify, sorting the value held at index i + MaxHeapify(array, i, heapSize); + } +} + +void HeapSort(std::vector &array) +{ + // Track the size of the heap + // + Since HeapSort will remain on the call stack until all recursion returns + // + Pass this value by reference to track size of heap as modifiable value + int heapSize = array.size(); + // Create a maximum heap from the array using its size + BuildMaxHeap(array, heapSize); + // For each value within the max heap, starting from its final value + for (int i = array.size() - 1; i > 0; i--) { + // Swap the top value within the heap with the value at i + std::swap(array[0], array[i]); + // + heapSize--; + MaxHeapify(array, 0, heapSize); + } + +} + diff --git a/cpp/algorithms/sorting/heap/lib-heap.hpp b/cpp/algorithms/sorting/heap/lib-heap.hpp new file mode 100644 index 0000000..b5602ec --- /dev/null +++ b/cpp/algorithms/sorting/heap/lib-heap.hpp @@ -0,0 +1,27 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of heap sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#ifndef LIB_HEAP_HPP +#define LIB_HEAP_HPP + +#include + +#define ARRAY_LENGTH 10 + +size_t Parent(const size_t &index); +size_t Left(const size_t &index); +size_t Right(const size_t &index); + +void MaxHeapify(std::vector &array, size_t thisIndex, int &heapSize); + +void BuildMaxHeap(std::vector &array, int &heapSize); + +void HeapSort(std::vector &array); + +#endif // LIB_HEAP_HPP