Add new cpp/algorithms directory, include basic selection sort example

This commit is contained in:
Shaun Reed 2021-01-17 17:01:39 -05:00
parent 245e04e083
commit d4f6fb9d41
4 changed files with 127 additions and 0 deletions

View File

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

View File

@ -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 <iostream>
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;
}

View File

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

View File

@ -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 <iostream>
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;
}