From 2f94a59567e7233de9e8a062d83ee0a477c28b20 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 25 May 2021 09:57:16 -0400 Subject: [PATCH] Add example for bubble sort --- cpp/algorithms/sorting/CMakeLists.txt | 1 + cpp/algorithms/sorting/bubble/CMakeLists.txt | 17 +++++++++ cpp/algorithms/sorting/bubble/bubble-sort.cpp | 37 +++++++++++++++++++ cpp/algorithms/sorting/bubble/lib-bubble.cpp | 29 +++++++++++++++ cpp/algorithms/sorting/bubble/lib-bubble.hpp | 19 ++++++++++ 5 files changed, 103 insertions(+) create mode 100644 cpp/algorithms/sorting/bubble/CMakeLists.txt create mode 100644 cpp/algorithms/sorting/bubble/bubble-sort.cpp create mode 100644 cpp/algorithms/sorting/bubble/lib-bubble.cpp create mode 100644 cpp/algorithms/sorting/bubble/lib-bubble.hpp diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index e44f245..4c85cf2 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -18,3 +18,4 @@ project ( add_subdirectory(merge) add_subdirectory(selection) add_subdirectory(insertion) +add_subdirectory(bubble) diff --git a/cpp/algorithms/sorting/bubble/CMakeLists.txt b/cpp/algorithms/sorting/bubble/CMakeLists.txt new file mode 100644 index 0000000..8827364 --- /dev/null +++ b/cpp/algorithms/sorting/bubble/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 bubble sort ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### + +cmake_minimum_required(VERSION 3.16) +project(BubbleSort LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(bubble-sort "bubble-sort.cpp") + +add_library(lib-bubble "lib-bubble.cpp") +target_link_libraries(bubble-sort lib-bubble) diff --git a/cpp/algorithms/sorting/bubble/bubble-sort.cpp b/cpp/algorithms/sorting/bubble/bubble-sort.cpp new file mode 100644 index 0000000..721db2a --- /dev/null +++ b/cpp/algorithms/sorting/bubble/bubble-sort.cpp @@ -0,0 +1,37 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of bubble sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-bubble.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); + + BubbleSort(array); + + std::cout << "Sorted array: \n"; + print(array); +} diff --git a/cpp/algorithms/sorting/bubble/lib-bubble.cpp b/cpp/algorithms/sorting/bubble/lib-bubble.cpp new file mode 100644 index 0000000..7e467e1 --- /dev/null +++ b/cpp/algorithms/sorting/bubble/lib-bubble.cpp @@ -0,0 +1,29 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of bubble sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-bubble.hpp" + +#include +#include + +void BubbleSort(std::vector &array) +{ + // For each value within the set, starting at 0 + for (int sortedPivot = 0; sortedPivot < array.size(); sortedPivot++) { + // Check every other remaining value in the set + for (int j = array.size() - 1; j > sortedPivot; j--) { + // Swap if the value at j is less than the value before it + if (array[j] < array[j - 1]) { + std::swap(array[j], array[j - 1]); + } + } + // Increment sortedPivot, marking the lhs portion of the set as sorted + } +} + diff --git a/cpp/algorithms/sorting/bubble/lib-bubble.hpp b/cpp/algorithms/sorting/bubble/lib-bubble.hpp new file mode 100644 index 0000000..886ca42 --- /dev/null +++ b/cpp/algorithms/sorting/bubble/lib-bubble.hpp @@ -0,0 +1,19 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of bubble sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#ifndef LIB_BUBBLE_HPP +#define LIB_BUBBLE_HPP + +#include + +#define ARRAY_LENGTH 10 + +void BubbleSort(std::vector &array); + +#endif // LIB_BUBBLE_HPP