From 8e14b25fa36042f89d13cc456d71f02f41ef6caa Mon Sep 17 00:00:00 2001 From: Mertcan Davulcu <54983926+mertcandav@users.noreply.github.com> Date: Wed, 30 Sep 2020 20:21:25 +0300 Subject: [PATCH] Added strand sort algorithm (#1081) * Create strand_sort.cpp * Update strand_sort.cpp * Update strand_sort.cpp * Delete strand_sort.cpp * Create strand_sort.cpp * Update sorting/strand_sort.cpp Co-authored-by: David Leal * Update strand_sort.cpp * Update strand_sort.cpp * Update sorting/strand_sort.cpp Co-authored-by: David Leal * Update sorting/strand_sort.cpp Co-authored-by: David Leal * Update strand_sort.cpp * Update sorting/strand_sort.cpp Co-authored-by: David Leal * Update sorting/strand_sort.cpp Co-authored-by: David Leal * Update sorting/strand_sort.cpp Co-authored-by: David Leal * Update sorting/strand_sort.cpp Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * Update sorting/strand_sort.cpp Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * Update sorting/strand_sort.cpp Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * Update strand_sort.cpp * Update strand_sort.cpp * updating DIRECTORY.md Co-authored-by: David Leal Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + sorting/strand_sort.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 sorting/strand_sort.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 60840a60d..e68f68aec 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -250,6 +250,7 @@ * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort.cpp) * [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp) * [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/slow_sort.cpp) + * [Strand Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/strand_sort.cpp) * [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp) * [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/tim_sort.cpp) diff --git a/sorting/strand_sort.cpp b/sorting/strand_sort.cpp new file mode 100644 index 000000000..732a28acc --- /dev/null +++ b/sorting/strand_sort.cpp @@ -0,0 +1,87 @@ +/** + * @file strand_sort.cpp + * @brief Implementation of [Strand Sort](https://en.wikipedia.org/wiki/Strand_sort) algorithm. + * + * @details + * Strand Sort is a sorting algorithm that works in \f$O(n)\f$ time if list is already sorted and works in \f$O(n^2)\f$ in worst case. + * + * It is passed over the array to be sorted once and the ascending (sequential) numbers are taken. + * After the first iteration, the sequential sub-array is put on the empty sorted array. + * The main sequence is passed over again and a new sub-sequence is created in order. + * Now that the sorted array is not empty, the newly extracted substring is merged with the sorted array. + * Repeat types 3 and 4 until the sub-sequence and main sequence are empty. + * + * @author [Mertcan Davulcu](https://github.com/mertcandav) + */ +#include +#include + +/** + * @namespace sorting + * @brief Sorting algorithms + */ +namespace sorting { + /** + * @namespace strand + * @brief Functions for [Strand Sort](https://en.wikipedia.org/wiki/Strand_sort) algorithm + */ + namespace strand { + /** + * @brief Apply sorting + * @tparam element type of list + * @param lst List to be sorted + * @returns Sorted list instance + */ + template + std::list strand_sort(std::list lst) { + if (lst.size() < 2) { // Returns list if empty or contains only one element + return lst; // Returns list + } + std::list result; // Define new "result" named list instance. + std::list sorted; // Define new "sorted" named list instance. + while(!lst.empty()) /* if lst is not empty */ { + sorted.push_back(lst.front()); // Adds the first element of "lst" list to the bottom of the "sorted" list. + lst.pop_front(); // Remove first element of "lst" list. + for (auto it = lst.begin(); it != lst.end(); ) { // Return the loop as long as the current iterator is not equal to the last literator of the "lst" list. + if (sorted.back() <= *it) { // If the last reference of the "sorted" list is less than or equal to the current iterator reference. + sorted.push_back(*it); // Adds the iterator retrieved in the loop under the "sorted" list. + it = lst.erase(it); // Deletes the element with the current iterator and assigns the deleted element to the iterator. + } else { + it++; // Next iterator. + } + } + result.merge(sorted); // Merge "result" list with "sorted" list. + } + return result; // Returns sorted list + } + } // namespace strand +} // namespace sorting + +/** + * @brief Function for testing + * @return N/A + */ +static void test() { + std::list lst = { -333, 525, 1, 0, 94, 52, 33 }; + + std::cout << "Before: "; + for(auto item: lst) { + std::cout << item << " "; + } + + lst = sorting::strand::strand_sort(lst); // Sort list. + + std::cout << "\nAfter: "; + for(auto item: lst) { + std::cout << item << " "; + } +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); + return 0; +}