From fa4407e74af7c2b0fedf27740ef045b85cdfe90b Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sun, 23 May 2021 10:57:19 -0400 Subject: [PATCH] Update selection sort example --- .../sorting/selection/CMakeLists.txt | 2 +- .../sorting/selection/lib-select.cpp | 43 +++++-------------- cpp/algorithms/sorting/selection/lib-select.h | 17 ++------ .../sorting/selection/select-sort.cpp | 27 +++++++++--- 4 files changed, 34 insertions(+), 55 deletions(-) diff --git a/cpp/algorithms/sorting/selection/CMakeLists.txt b/cpp/algorithms/sorting/selection/CMakeLists.txt index eeabcac..d5ff95c 100644 --- a/cpp/algorithms/sorting/selection/CMakeLists.txt +++ b/cpp/algorithms/sorting/selection/CMakeLists.txt @@ -1,6 +1,6 @@ ############################################################################### ## Author: Shaun Reed ## -## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## About: A basic CMakeLists configuration to practice selection sort ## ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## diff --git a/cpp/algorithms/sorting/selection/lib-select.cpp b/cpp/algorithms/sorting/selection/lib-select.cpp index 3dd61a3..0bb68f9 100644 --- a/cpp/algorithms/sorting/selection/lib-select.cpp +++ b/cpp/algorithms/sorting/selection/lib-select.cpp @@ -1,6 +1,6 @@ /*############################################################################# ## Author: Shaun Reed ## -## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## Legal: All Content (c) 2021 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 ## @@ -9,41 +9,18 @@ #include "lib-select.h" -#include +#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); +void SelectionSort(std::vector &arr) { + for (int leftIndex = 0; leftIndex < arr.size(); leftIndex++) { + // Get the index for the minimum number in the unsorted set + int min = leftIndex; + for (int i = leftIndex; i < arr.size(); i++) { + // Check if value at i is smaller than value at min index + min = (arr[min] > arr[i]) ? i : min; // Update min value to i if true + } // 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 index b71cc6e..c826062 100644 --- a/cpp/algorithms/sorting/selection/lib-select.h +++ b/cpp/algorithms/sorting/selection/lib-select.h @@ -1,6 +1,6 @@ /*############################################################################# ## Author: Shaun Reed ## -## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## Legal: All Content (c) 2021 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 ## @@ -12,23 +12,12 @@ #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(); +#include /** 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[]); +void SelectionSort(std::vector &arr); #endif // LIB_SELECT_H diff --git a/cpp/algorithms/sorting/selection/select-sort.cpp b/cpp/algorithms/sorting/selection/select-sort.cpp index d692bdf..46c5301 100644 --- a/cpp/algorithms/sorting/selection/select-sort.cpp +++ b/cpp/algorithms/sorting/selection/select-sort.cpp @@ -1,6 +1,6 @@ /*############################################################################# ## Author: Shaun Reed ## -## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## Legal: All Content (c) 2021 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 ## @@ -9,19 +9,32 @@ #include "lib-select.h" +#include #include +#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(); + // Create a random vector with size ARRAY_LENGTH set within lib-select.cpp + std::srand(time(nullptr)); + std::vector array(ARRAY_LENGTH, 0); + std::generate(array.begin(), array.end(), [](){return rand() % 1000;}); + // Lambda for printing a vector + auto print = [](std::vector const &array)->void { + for (const auto &i : array) std::cout << i << ", "; + std::cout << std::endl; + }; + + // Print vector before we sort it std::cout << "Unsorted array: \n"; - PrintArray(numArray); + print(array); - // Sort the array and print the new contents - SelectionSort(numArray); + // Sort the vector + SelectionSort(array); + + // Print sorted vector std::cout << "\nSorted array: \n"; - PrintArray(numArray); + print(array); return 0; }