From d4f6fb9d4157e7fffc1d02e80f489054922f2f73 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sun, 17 Jan 2021 17:01:39 -0500 Subject: [PATCH] Add new cpp/algorithms directory, include basic selection sort example --- .../sorting/selection/CMakeLists.txt | 17 +++++++ .../sorting/selection/lib-select.cpp | 49 +++++++++++++++++++ cpp/algorithms/sorting/selection/lib-select.h | 34 +++++++++++++ .../sorting/selection/select-sort.cpp | 27 ++++++++++ 4 files changed, 127 insertions(+) create mode 100644 cpp/algorithms/sorting/selection/CMakeLists.txt create mode 100644 cpp/algorithms/sorting/selection/lib-select.cpp create mode 100644 cpp/algorithms/sorting/selection/lib-select.h create mode 100644 cpp/algorithms/sorting/selection/select-sort.cpp diff --git a/cpp/algorithms/sorting/selection/CMakeLists.txt b/cpp/algorithms/sorting/selection/CMakeLists.txt new file mode 100644 index 0000000..4a9fab2 --- /dev/null +++ b/cpp/algorithms/sorting/selection/CMakeLists.txt @@ -0,0 +1,17 @@ +############################################################################### +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: A basic CMakeLists configuration to practice selection sort ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### + +cmake_minimum_required(VERSION 3.17) +project(SelectionSort LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(select-sort "select-sort.cpp") + +add_library(lib-select "lib-select.cpp") +target_link_libraries(select-sort lib-select) diff --git a/cpp/algorithms/sorting/selection/lib-select.cpp b/cpp/algorithms/sorting/selection/lib-select.cpp new file mode 100644 index 0000000..3dd61a3 --- /dev/null +++ b/cpp/algorithms/sorting/selection/lib-select.cpp @@ -0,0 +1,49 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: An example implementation of selection sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-select.h" + +#include + +static int FindMinIndex(int const arr[], int start) { + // Assume we are at the minimum index until another is found + int min = start; + for (int i = start; i < 10; i++) { + if (arr[min] > arr[i]) min = i; + } + return min; +} + +int * RandomArray() { + static int newArray[ARRAY_LENGTH]; + srand(time(NULL)); + + int randMax = 1000; + for (int i = 0; i < ARRAY_LENGTH; i++) { + newArray[i] = random() % randMax; + } + return newArray; +} + +void SelectionSort(int arr[]) { + for (int leftIndex = 0; leftIndex < ARRAY_LENGTH; leftIndex++) { + // Check for a different minimum in the unsorted portion of the array + int min = FindMinIndex(arr, leftIndex); + + // If the minimum index has changed from it's origin, swap the elements + if (min != leftIndex) std::swap(arr[leftIndex], arr[min]); + } +} + +void PrintArray(int arr[]) { + for (int i = 0; i < ARRAY_LENGTH; i++) { + std::cout << arr[i] << std::endl; + } + std::cout << std::endl; +} diff --git a/cpp/algorithms/sorting/selection/lib-select.h b/cpp/algorithms/sorting/selection/lib-select.h new file mode 100644 index 0000000..b71cc6e --- /dev/null +++ b/cpp/algorithms/sorting/selection/lib-select.h @@ -0,0 +1,34 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: An example implementation of selection sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#ifndef LIB_SELECT_H +#define LIB_SELECT_H + +#define ARRAY_LENGTH 10 + +/** RandomArray + * Returns an array automatically filled with random values + * Sizes array to match length setting ARRAY_LENGTH + * @return A pointer to a static array + */ +int * RandomArray(); + +/** SelectionSort + * Sorts an array of integers in ascending order + * @param arr The array to sort + */ +void SelectionSort(int arr[]); + +/** PrintArray + * Prints an array based on the define ARRAY_LENGTH + * @param arr The array to print + */ +void PrintArray(int arr[]); + +#endif // LIB_SELECT_H diff --git a/cpp/algorithms/sorting/selection/select-sort.cpp b/cpp/algorithms/sorting/selection/select-sort.cpp new file mode 100644 index 0000000..d692bdf --- /dev/null +++ b/cpp/algorithms/sorting/selection/select-sort.cpp @@ -0,0 +1,27 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: An example implementation of selection sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-select.h" + +#include + +int main(const int argc, char const * argv[]) { + // Create a random array based on ARRAY_LENGTH setting in lib-select.cpp + int * numArray = RandomArray(); + + std::cout << "Unsorted array: \n"; + PrintArray(numArray); + + // Sort the array and print the new contents + SelectionSort(numArray); + std::cout << "\nSorted array: \n"; + PrintArray(numArray); + + return 0; +}