From 8f70278ac667206186c0bc92ea4c9022e1ec6d0b Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 25 May 2021 00:00:44 -0400 Subject: [PATCH] Add example for insertion sort --- cpp/algorithms/sorting/CMakeLists.txt | 1 + .../sorting/insertion/CMakeLists.txt | 17 ++++++++ .../sorting/insertion/insertion-sort.cpp | 37 ++++++++++++++++++ .../sorting/insertion/lib-insertion.cpp | 39 +++++++++++++++++++ .../sorting/insertion/lib-insertion.hpp | 19 +++++++++ 5 files changed, 113 insertions(+) create mode 100644 cpp/algorithms/sorting/insertion/CMakeLists.txt create mode 100644 cpp/algorithms/sorting/insertion/insertion-sort.cpp create mode 100644 cpp/algorithms/sorting/insertion/lib-insertion.cpp create mode 100644 cpp/algorithms/sorting/insertion/lib-insertion.hpp diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index ac988d6..e44f245 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -17,3 +17,4 @@ project ( add_subdirectory(merge) add_subdirectory(selection) +add_subdirectory(insertion) diff --git a/cpp/algorithms/sorting/insertion/CMakeLists.txt b/cpp/algorithms/sorting/insertion/CMakeLists.txt new file mode 100644 index 0000000..5cec75f --- /dev/null +++ b/cpp/algorithms/sorting/insertion/CMakeLists.txt @@ -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) diff --git a/cpp/algorithms/sorting/insertion/insertion-sort.cpp b/cpp/algorithms/sorting/insertion/insertion-sort.cpp new file mode 100644 index 0000000..c405f17 --- /dev/null +++ b/cpp/algorithms/sorting/insertion/insertion-sort.cpp @@ -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 +#include +#include +#include + +int main(const int argc, const char * argv[]) +{ + srand(time(nullptr)); + std::vector array(ARRAY_LENGTH); + std::generate(array.begin(), array.end(), [](){ return rand() % 1000;}); + + auto print = [](std::vector 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); +} diff --git a/cpp/algorithms/sorting/insertion/lib-insertion.cpp b/cpp/algorithms/sorting/insertion/lib-insertion.cpp new file mode 100644 index 0000000..f6f2708 --- /dev/null +++ b/cpp/algorithms/sorting/insertion/lib-insertion.cpp @@ -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 +#include + +void InsertionSort(std::vector &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; + + } +} + diff --git a/cpp/algorithms/sorting/insertion/lib-insertion.hpp b/cpp/algorithms/sorting/insertion/lib-insertion.hpp new file mode 100644 index 0000000..c0409ce --- /dev/null +++ b/cpp/algorithms/sorting/insertion/lib-insertion.hpp @@ -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 + +#define ARRAY_LENGTH 10 + +void InsertionSort(std::vector &array); + +#endif // LIB_INSERTION_HPP