-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added Iterative Quick Sort #2684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
realstealthninja
merged 14 commits into
TheAlgorithms:master
from
sebe324:iterative_quick_sort
Sep 4, 2024
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
93f5432
feat: Added iterative quick sort using stack
sebe324 6a76f9f
fix: Forgot to add @param for sort function
sebe324 b968549
Update sorting/quick_sort_iterative.cpp
realstealthninja 338d22c
Update sorting/quick_sort_iterative.cpp
realstealthninja 9f0c8eb
Update sorting/quick_sort_iterative.cpp
realstealthninja 8982255
style: space b/w for and comment
realstealthninja f8ac592
Merge branch 'master' into iterative_quick_sort
realstealthninja 6ab9e08
Merge branch 'master' into iterative_quick_sort
realstealthninja 67f93af
Update sorting/quick_sort_iterative.cpp
sebe324 db5f634
Update sorting/quick_sort_iterative.cpp
sebe324 c9daebe
Merge branch 'master' into iterative_quick_sort
realstealthninja a78b62c
Merge branch 'TheAlgorithms:master' into iterative_quick_sort
sebe324 e279be5
fixed namespace error
sebe324 e5236ac
Merge branch 'master' into iterative_quick_sort
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* @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 <iostream> //for std::cout | ||
#include <vector> //for std::vector | ||
#include <stack> //for std::stack | ||
#include <algorithm> //for std::is_sorted | ||
#include <cassert> //for assert | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
sebe324 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @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<int> &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. | ||
* @param arr array to be sorted | ||
* @return void | ||
*/ | ||
void iterativeQuickSort(std::vector<int> &arr) | ||
{ | ||
std::stack<int> 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<end) | ||
{ | ||
stack.push(pivotIndex+1); | ||
stack.push(end); | ||
} | ||
} | ||
} | ||
|
||
sebe324 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
void tests() | ||
{ | ||
//TEST 1 - Positive numbers | ||
std::vector<int> 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<<x<<","; | ||
std::cout<<"\n"; | ||
iterativeQuickSort(case1); | ||
assert(std::is_sorted(std::begin(case1),std::end(case1))); | ||
std::cout<<"Test 1 succesful!\n"; | ||
std::cout<<"After: \n"; | ||
for(auto x : case1) std::cout<<x<<","; | ||
std::cout<<"\n"; | ||
|
||
//TEST 2 - Negative numbers | ||
std::vector<int> 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<<x<<","; | ||
std::cout<<"\n"; | ||
iterativeQuickSort(case2); | ||
assert(std::is_sorted(std::begin(case2),std::end(case2))); | ||
std::cout<<"Test 2 succesful!\n"; | ||
std::cout<<"After: \n"; | ||
for(auto x : case2) std::cout<<x<<","; | ||
std::cout<<"\n"; | ||
} | ||
|
||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() | ||
{ | ||
tests(); | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.