From a7d11c559ebf74cc506439381c2237d844b470ef Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sat, 29 May 2021 10:23:01 -0400 Subject: [PATCH] Add example for counting sort --- cpp/algorithms/sorting/CMakeLists.txt | 1 + cpp/algorithms/sorting/count/CMakeLists.txt | 17 +++++++ .../sorting/count/counting-sort.cpp | 40 +++++++++++++++ cpp/algorithms/sorting/count/lib-counting.cpp | 49 +++++++++++++++++++ cpp/algorithms/sorting/count/lib-counting.hpp | 21 ++++++++ 5 files changed, 128 insertions(+) create mode 100644 cpp/algorithms/sorting/count/CMakeLists.txt create mode 100644 cpp/algorithms/sorting/count/counting-sort.cpp create mode 100644 cpp/algorithms/sorting/count/lib-counting.cpp create mode 100644 cpp/algorithms/sorting/count/lib-counting.hpp diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index 37ff311..508f191 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -21,3 +21,4 @@ add_subdirectory(insertion) add_subdirectory(bubble) add_subdirectory(heap) add_subdirectory(quick) +add_subdirectory(count) diff --git a/cpp/algorithms/sorting/count/CMakeLists.txt b/cpp/algorithms/sorting/count/CMakeLists.txt new file mode 100644 index 0000000..4d216be --- /dev/null +++ b/cpp/algorithms/sorting/count/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 counting sort ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### + +cmake_minimum_required(VERSION 3.16) +project(CountingSort LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(counting-sort "counting-sort.cpp") + +add_library(lib-counting "lib-counting.cpp") +target_link_libraries(counting-sort lib-counting) diff --git a/cpp/algorithms/sorting/count/counting-sort.cpp b/cpp/algorithms/sorting/count/counting-sort.cpp new file mode 100644 index 0000000..92c0619 --- /dev/null +++ b/cpp/algorithms/sorting/count/counting-sort.cpp @@ -0,0 +1,40 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of counting sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-counting.hpp" + +#include +#include +#include +#include + + +int main(const int argc, const char * argv[]) +{ + srand(time(nullptr)); + std::vector array(ARRAY_LENGTH); + // For this example, we limit the maximum value to 10 + // + This helps to provide duplicates so we have > 1 count of some values + std::generate(array.begin(), array.end(), [](){ return rand() % 10;}); + + auto print = [](std::vector array) { + for (const auto &i : array) { + std::cout << i << ", "; + } + std::cout << std::endl; + }; + + std::cout << "Unsorted array: \n"; + print(array); + + CountingSort(array); + + std::cout << "Sorted array: \n"; + print(array); +} diff --git a/cpp/algorithms/sorting/count/lib-counting.cpp b/cpp/algorithms/sorting/count/lib-counting.cpp new file mode 100644 index 0000000..8558e2e --- /dev/null +++ b/cpp/algorithms/sorting/count/lib-counting.cpp @@ -0,0 +1,49 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of counting sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-counting.hpp" + +#include +#include +#include + + +void CountingSort(std::vector &array) +{ + std::vector sortedArray(ARRAY_LENGTH); + // Find the maximum value within the array to sort + int32_t maxValue = INT32_MIN; + for (const auto &val : array) maxValue = (val > maxValue) ? val : maxValue; + + // Create an array with element for all *values* within the range of the set + // + Not the size of the array to sort, but instead a element of + // + tempArray[val] = 0; for each val within the array we want to sort + // Add one to maxValue to make up for zero index; 0's are possible, too! + std::vector tempArray(maxValue + 1, 0); + + // Count the occurrences of each value within the array to sort + // + Store the end result as tempArray[value] = count; + for (const auto &val : array) tempArray[val] += 1; + + // Count the values less than or equal to each element of tempArray + // + Since each element stores its own count, just add the count at index i-1 + for (size_t i = 1; i <= maxValue; i++) { + tempArray[i] += tempArray[i - 1]; + } + + for (ssize_t arrayIndex = array.size() - 1; arrayIndex >= 0; arrayIndex--) { + // Store as references; Changes reflect on actual values within each array + const int &arrayValue = array[arrayIndex]; + int &valueCount = tempArray[arrayValue]; + sortedArray[valueCount - 1] = arrayValue; + valueCount = valueCount - 1; + } + + array = sortedArray; +} diff --git a/cpp/algorithms/sorting/count/lib-counting.hpp b/cpp/algorithms/sorting/count/lib-counting.hpp new file mode 100644 index 0000000..86e4e87 --- /dev/null +++ b/cpp/algorithms/sorting/count/lib-counting.hpp @@ -0,0 +1,21 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of counting sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#ifndef LIB_COUNT_HPP +#define LIB_COUNT_HPP + +#include + +// For this example double the size of the array so we have more values to count +#define ARRAY_LENGTH 20 + + +void CountingSort(std::vector &array); + +#endif // LIB_COUNT_HPP