Update selection sort example

This commit is contained in:
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: 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;
}