Add example for insertion sort

This commit is contained in:
Shaun Reed 2021-05-25 00:00:44 -04:00
parent 82effe5203
commit 8f70278ac6
5 changed files with 113 additions and 0 deletions

View File

@ -17,3 +17,4 @@ project (
add_subdirectory(merge)
add_subdirectory(selection)
add_subdirectory(insertion)

View File

@ -0,0 +1,17 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: A basic CMakeLists configuration to practice insertion sort ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
cmake_minimum_required(VERSION 3.16)
project(InsertionSort LANGUAGES CXX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(insertion-sort "insertion-sort.cpp")
add_library(lib-insertion "lib-insertion.cpp")
target_link_libraries(insertion-sort lib-insertion)

View File

@ -0,0 +1,37 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of insertion sort using a custom library ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#include "lib-insertion.hpp"
#include <algorithm>
#include <vector>
#include <random>
#include <iostream>
int main(const int argc, const char * argv[])
{
srand(time(nullptr));
std::vector<int> array(ARRAY_LENGTH);
std::generate(array.begin(), array.end(), [](){ return rand() % 1000;});
auto print = [](std::vector<int> array) {
for (const auto &i : array) {
std::cout << i << ", ";
}
std::cout << std::endl;
};
std::cout << "Unsorted array: \n";
print(array);
InsertionSort(array);
std::cout << "Sorted array: \n";
print(array);
}

View File

@ -0,0 +1,39 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of insertion sort using a custom library ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#include "lib-insertion.hpp"
#include <algorithm>
#include <vector>
void InsertionSort(std::vector<int> &array)
{
// For each value, move left until we find sortedPosition for keyValue
// + Starting with keyValue at array[1], to check sortedPosition at array[0]
for (int keyIndex = 1; keyIndex <= array.size(); keyIndex++) {
// Save the current key value
// + We will look for the sorted position of this value
const int keyValue = array[keyIndex];
// First, check if the value at the previous index is > keyValue
int sortedPosition = keyIndex - 1;
// While array[sortedPosition] > keyValue
while (sortedPosition >= 0 && array[sortedPosition] > keyValue) {
// The value in the lower index is larger than the keyValue
// + Move the value at sortedPosition to the right (ascending order)
array[sortedPosition + 1] = array[sortedPosition];
// Decrement sortedPosition so we can check the next value
sortedPosition--;
}
// Insert the keyValue in its final sorted position
// + Once array[sortedPosition] < keyValue, keyValue belongs one index right
array[sortedPosition + 1] = keyValue;
}
}

View File

@ -0,0 +1,19 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: An example implementation of insertion sort using a custom library ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
###############################################################################
*/
#ifndef LIB_INSERTION_HPP
#define LIB_INSERTION_HPP
#include <vector>
#define ARRAY_LENGTH 10
void InsertionSort(std::vector<int> &array);
#endif // LIB_INSERTION_HPP