Update selection sort example
This commit is contained in:
parent
54431f3e1e
commit
fa4407e74a
|
@ -1,6 +1,6 @@
|
||||||
###############################################################################
|
###############################################################################
|
||||||
## Author: Shaun Reed ##
|
## 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 ##
|
## About: A basic CMakeLists configuration to practice selection sort ##
|
||||||
## ##
|
## ##
|
||||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## Author: Shaun Reed ##
|
## 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 ##
|
## About: An example implementation of selection sort using a custom library ##
|
||||||
## ##
|
## ##
|
||||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
@ -9,41 +9,18 @@
|
||||||
|
|
||||||
#include "lib-select.h"
|
#include "lib-select.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <vector>
|
||||||
|
|
||||||
static int FindMinIndex(int const arr[], int start) {
|
void SelectionSort(std::vector<int> &arr) {
|
||||||
// Assume we are at the minimum index until another is found
|
for (int leftIndex = 0; leftIndex < arr.size(); leftIndex++) {
|
||||||
int min = start;
|
// Get the index for the minimum number in the unsorted set
|
||||||
for (int i = start; i < 10; i++) {
|
int min = leftIndex;
|
||||||
if (arr[min] > arr[i]) min = i;
|
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
|
||||||
}
|
}
|
||||||
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 the minimum index has changed from it's origin, swap the elements
|
||||||
if (min != leftIndex) std::swap(arr[leftIndex], arr[min]);
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## Author: Shaun Reed ##
|
## 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 ##
|
## About: An example implementation of selection sort using a custom library ##
|
||||||
## ##
|
## ##
|
||||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
@ -12,23 +12,12 @@
|
||||||
|
|
||||||
#define ARRAY_LENGTH 10
|
#define ARRAY_LENGTH 10
|
||||||
|
|
||||||
/** RandomArray
|
#include <vector>
|
||||||
* 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
|
/** SelectionSort
|
||||||
* Sorts an array of integers in ascending order
|
* Sorts an array of integers in ascending order
|
||||||
* @param arr The array to sort
|
* @param arr The array to sort
|
||||||
*/
|
*/
|
||||||
void SelectionSort(int arr[]);
|
void SelectionSort(std::vector<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
|
#endif // LIB_SELECT_H
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## Author: Shaun Reed ##
|
## 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 ##
|
## About: An example implementation of selection sort using a custom library ##
|
||||||
## ##
|
## ##
|
||||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
@ -9,19 +9,32 @@
|
||||||
|
|
||||||
#include "lib-select.h"
|
#include "lib-select.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
int main(const int argc, char const * argv[]) {
|
int main(const int argc, char const * argv[]) {
|
||||||
// Create a random array based on ARRAY_LENGTH setting in lib-select.cpp
|
// Create a random vector with size ARRAY_LENGTH set within lib-select.cpp
|
||||||
int * numArray = RandomArray();
|
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";
|
std::cout << "Unsorted array: \n";
|
||||||
PrintArray(numArray);
|
print(array);
|
||||||
|
|
||||||
// Sort the array and print the new contents
|
// Sort the vector
|
||||||
SelectionSort(numArray);
|
SelectionSort(array);
|
||||||
|
|
||||||
|
// Print sorted vector
|
||||||
std::cout << "\nSorted array: \n";
|
std::cout << "\nSorted array: \n";
|
||||||
PrintArray(numArray);
|
print(array);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue