Update selection sort example

This commit is contained in:
Shaun Reed 2021-05-23 10:57:19 -04:00
parent 54431f3e1e
commit fa4407e74a
4 changed files with 34 additions and 55 deletions

View File

@ -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 ##

View File

@ -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 <iostream>
#include <vector>
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<int> &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;
}

View File

@ -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 <vector>
/** 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<int> &arr);
#endif // LIB_SELECT_H

View File

@ -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 <algorithm>
#include <iostream>
#include <vector>
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<int> array(ARRAY_LENGTH, 0);
std::generate(array.begin(), array.end(), [](){return rand() % 1000;});
// Lambda for printing a vector
auto print = [](std::vector<int> 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;
}