From 33843eaaa15e0077d41a77c3ef6618537c261cc2 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Wed, 26 May 2021 14:12:27 -0400 Subject: [PATCH] Add example for quick sort --- cpp/algorithms/sorting/CMakeLists.txt | 1 + cpp/algorithms/sorting/quick/CMakeLists.txt | 17 ++++++ cpp/algorithms/sorting/quick/lib-quick.cpp | 63 +++++++++++++++++++++ cpp/algorithms/sorting/quick/lib-quick.hpp | 21 +++++++ cpp/algorithms/sorting/quick/quick-sort.cpp | 37 ++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 cpp/algorithms/sorting/quick/CMakeLists.txt create mode 100644 cpp/algorithms/sorting/quick/lib-quick.cpp create mode 100644 cpp/algorithms/sorting/quick/lib-quick.hpp create mode 100644 cpp/algorithms/sorting/quick/quick-sort.cpp diff --git a/cpp/algorithms/sorting/CMakeLists.txt b/cpp/algorithms/sorting/CMakeLists.txt index fae1404..37ff311 100644 --- a/cpp/algorithms/sorting/CMakeLists.txt +++ b/cpp/algorithms/sorting/CMakeLists.txt @@ -20,3 +20,4 @@ add_subdirectory(selection) add_subdirectory(insertion) add_subdirectory(bubble) add_subdirectory(heap) +add_subdirectory(quick) diff --git a/cpp/algorithms/sorting/quick/CMakeLists.txt b/cpp/algorithms/sorting/quick/CMakeLists.txt new file mode 100644 index 0000000..1c98edc --- /dev/null +++ b/cpp/algorithms/sorting/quick/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 quick sort ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### + +cmake_minimum_required(VERSION 3.16) +project(QuickSort LANGUAGES CXX) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(quick-sort "quick-sort.cpp") + +add_library(lib-quick "lib-quick.cpp") +target_link_libraries(quick-sort lib-quick) diff --git a/cpp/algorithms/sorting/quick/lib-quick.cpp b/cpp/algorithms/sorting/quick/lib-quick.cpp new file mode 100644 index 0000000..e6a7a4a --- /dev/null +++ b/cpp/algorithms/sorting/quick/lib-quick.cpp @@ -0,0 +1,63 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of quick sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-quick.hpp" + +#include +#include + + +void QuickSort(std::vector &array, size_t begin, size_t end) +{ + // Base case breaks out of recursion once begin and end converge on each other + if (begin >= end) return; + + // Search through array, finding the correct position for array[end] + // + Returns the index *after* the new position for array[end] + size_t pivot = Partition(array, begin, end); + // array[end] is now at array[pivot] + + // Continue to sort the lhs partition of the set + // + Using pivot-1 as the end index, we only consider the unsorted lhs portion + QuickSort(array, begin, (pivot > 0) ? pivot - 1 : pivot); + // In the top-level call to QuickSort, we don't reach this point until + // + all values in the lhs portion of the array are < array[pivot] + + // At this point, pivot+1 now effectively represents the begin of unsorted rhs + // + From pivot+1 to end, the array is currently unsorted + + // Continue to sort the rhs partition of the array + // + Using pivot+1 as the begin index, we only consider the unsorted rhs portion + QuickSort(array, pivot + 1, end); + + // The entire array, or the entire partition, is now sorted + // + Return to previous QuickSort on call stack, or return to main +} + +size_t Partition(std::vector &array, size_t begin, size_t end) +{ + // Find the correct position for keyValue within the array partition + // + Partition is within the range of array[begin ... end] + int keyValue = array[end]; + + // As we find values smaller than keyValue, track the last used index + // + Return this value when done, so we know where the lhs partition ends + ssize_t lhsIndex = begin - 1; + // For each value within this partition, check for values < keyValue + for (int j = begin; j <= end - 1; j++) { + if (array[j] <= keyValue) { + // Swap all values < keyValue into the lhs portion of array + std::swap(array[++lhsIndex], array[j]); + } + } + // Swap keyValue into lhsIndex+1, since we know all values before are smaller + std::swap(array[++lhsIndex], array[end]); + // Return the last used index for the lhs partition + return lhsIndex; +} diff --git a/cpp/algorithms/sorting/quick/lib-quick.hpp b/cpp/algorithms/sorting/quick/lib-quick.hpp new file mode 100644 index 0000000..15c8855 --- /dev/null +++ b/cpp/algorithms/sorting/quick/lib-quick.hpp @@ -0,0 +1,21 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of quick sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#ifndef LIB_QUICK_HPP +#define LIB_QUICK_HPP + +#include + +#define ARRAY_LENGTH 10 + +void QuickSort(std::vector &array, size_t begin, size_t end); + +size_t Partition(std::vector &array, size_t begin, size_t end); + +#endif // LIB_QUICK_HPP diff --git a/cpp/algorithms/sorting/quick/quick-sort.cpp b/cpp/algorithms/sorting/quick/quick-sort.cpp new file mode 100644 index 0000000..2720c36 --- /dev/null +++ b/cpp/algorithms/sorting/quick/quick-sort.cpp @@ -0,0 +1,37 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: An example implementation of quick sort using a custom library ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################### +*/ + +#include "lib-quick.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); + + QuickSort(array, 0, array.size() - 1); + + std::cout << "Sorted array: \n"; + print(array); +}