From 93f54329f52f422f9a8919c4ed9a79f5343ca8b8 Mon Sep 17 00:00:00 2001 From: sebe324 Date: Thu, 21 Mar 2024 20:51:08 +0100 Subject: [PATCH 1/9] feat: Added iterative quick sort using stack --- sorting/quick_sort_iterative.cpp | 124 +++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 sorting/quick_sort_iterative.cpp diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp new file mode 100644 index 00000000000..18045644d6d --- /dev/null +++ b/sorting/quick_sort_iterative.cpp @@ -0,0 +1,124 @@ +/** + * @file + * @brief Quick Sort without recursion. This method uses the stack instead. + * Both recursive and iterative implementations have O(n log n) best case + * and O(n^2) worst case. + * @details + * https://stackoverflow.com/questions/12553238/quicksort-iterative-or-recursive + * https://en.wikipedia.org/wiki/Quicksort + * https://www.geeksforgeeks.org/iterative-quick-sort/ + * @author Sebe324 (https://github.com/sebe324) + */ + +#include //for std::cout +#include //for std::vector +#include //for std::stack +#include //for std::is_sorted +#include //for assert + +/** + * @brief The partition function sorts the array from + * start to end and uses the last element as the pivot. + * @param arr the array to be sorted + * @param start starting index + * @param end ending index + * @return int next index of the pivot + */ +int partition(std::vector &arr, int start, int end) +{ + int pivot = arr[end]; + int index = start - 1; + + for (int j = start; j < end; j++) { + if (arr[j] <= pivot) { + std::swap(arr[++index], arr[j]); + } + } + + std::swap(arr[index + 1], arr[end]); + return index + 1; +} + +/** + * @brief The main sorting function + * @details The iterative quick sort uses + * the stack instead of recursion for saving + * and restoring the environment between calls. + * It does not need the end and start params, because + * it is not recursive. + * @return void + */ +void iterativeQuickSort(std::vector &arr) +{ + std::stack stack; + int start = 0; + int end = arr.size()-1; + stack.push(start); + stack.push(end); + + while(!stack.empty()) + { + end = stack.top(); + stack.pop(); + start = stack.top(); + stack.pop(); + + int pivotIndex = partition(arr,start,end); + + if(pivotIndex -1 > start) + { + stack.push(start); + stack.push(pivotIndex-1); + } + + if(pivotIndex+1 case1={100,534,1000000,553,10,61,2000,238,2756,9,12,56,30}; + std::cout<<"TEST 1\n"; + std::cout<<"Before: \n"; + for(auto x : case1) std::cout< case2={-10,-2,-5,-2,-3746,-785,-123, -452, -32456}; + std::cout<<"TEST 2\n"; + std::cout<<"Before: \n"; + for(auto x : case2) std::cout< Date: Thu, 21 Mar 2024 21:04:45 +0100 Subject: [PATCH 2/9] fix: Forgot to add @param for sort function --- sorting/quick_sort_iterative.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index 18045644d6d..191a13b7c1a 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -46,6 +46,7 @@ int partition(std::vector &arr, int start, int end) * and restoring the environment between calls. * It does not need the end and start params, because * it is not recursive. + * @param arr array to be sorted * @return void */ void iterativeQuickSort(std::vector &arr) From b9685497f58b30ca71eca663522305be09207c37 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Wed, 21 Aug 2024 07:50:22 +0530 Subject: [PATCH 3/9] Update sorting/quick_sort_iterative.cpp --- sorting/quick_sort_iterative.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index 191a13b7c1a..ccfc0ec9a03 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -10,11 +10,11 @@ * @author Sebe324 (https://github.com/sebe324) */ -#include //for std::cout -#include //for std::vector -#include //for std::stack -#include //for std::is_sorted -#include //for assert +#include /// for std::cout +#include /// for std::vector +#include /// for std::stack +#include ///for std::is_sorted +#include /// for assert /** * @brief The partition function sorts the array from From 338d22c29f76193e955fc66430787d0816f1147d Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Wed, 21 Aug 2024 07:50:29 +0530 Subject: [PATCH 4/9] Update sorting/quick_sort_iterative.cpp --- sorting/quick_sort_iterative.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index ccfc0ec9a03..9d55d2f685d 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -7,7 +7,7 @@ * https://stackoverflow.com/questions/12553238/quicksort-iterative-or-recursive * https://en.wikipedia.org/wiki/Quicksort * https://www.geeksforgeeks.org/iterative-quick-sort/ - * @author Sebe324 (https://github.com/sebe324) + * @author [Sebe324](https://github.com/sebe324) */ #include /// for std::cout From 9f0c8eb80b93427239872da3033515e5570313ad Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Wed, 21 Aug 2024 07:51:42 +0530 Subject: [PATCH 5/9] Update sorting/quick_sort_iterative.cpp --- sorting/quick_sort_iterative.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index 9d55d2f685d..d1f94232bec 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -120,6 +120,6 @@ void tests() */ int main() { - tests(); + tests(); // run self test implementation return 0; } From 898225542d4b15081ff817a656d0a6754db9dd84 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Sat, 31 Aug 2024 08:19:29 +0530 Subject: [PATCH 6/9] style: space b/w for and comment --- sorting/quick_sort_iterative.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index d1f94232bec..44683e3fb1f 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -13,7 +13,7 @@ #include /// for std::cout #include /// for std::vector #include /// for std::stack -#include ///for std::is_sorted +#include /// for std::is_sorted #include /// for assert /** From 67f93af3c5f382563ebfe309b61060bd6ebbed4c Mon Sep 17 00:00:00 2001 From: Sebastian Skonieczny <58781463+sebe324@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:25:30 +0200 Subject: [PATCH 7/9] Update sorting/quick_sort_iterative.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- sorting/quick_sort_iterative.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index 44683e3fb1f..04ef757a081 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -16,6 +16,12 @@ #include /// for std::is_sorted #include /// for assert + +/** + * @namespace sorting + * @brief Sorting algorithms + */ +namespace sorting { /** * @brief The partition function sorts the array from * start to end and uses the last element as the pivot. From db5f634fd258dba062f254e959e28caa472e8f10 Mon Sep 17 00:00:00 2001 From: Sebastian Skonieczny <58781463+sebe324@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:25:38 +0200 Subject: [PATCH 8/9] Update sorting/quick_sort_iterative.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- sorting/quick_sort_iterative.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index 04ef757a081..b980e3d4f36 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -86,6 +86,7 @@ void iterativeQuickSort(std::vector &arr) } } +} // namespace sorting /** * @brief Self-test implementations * @returns void From e279be54f836adbfe048f959700236957e1ea6df Mon Sep 17 00:00:00 2001 From: sebe324 Date: Tue, 3 Sep 2024 17:29:29 +0200 Subject: [PATCH 9/9] fixed namespace error --- sorting/quick_sort_iterative.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorting/quick_sort_iterative.cpp b/sorting/quick_sort_iterative.cpp index b980e3d4f36..da506c9c024 100644 --- a/sorting/quick_sort_iterative.cpp +++ b/sorting/quick_sort_iterative.cpp @@ -99,7 +99,7 @@ void tests() std::cout<<"Before: \n"; for(auto x : case1) std::cout<