TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
quick_sort_iterative.cpp
Go to the documentation of this file.
1
12
13
#include <iostream>
14
#include <vector>
15
#include <stack>
16
#include <algorithm>
17
#include <cassert>
18
19
24
namespace
sorting
{
33
int
partition
(std::vector<int> &arr,
int
start,
int
end)
34
{
35
int
pivot = arr[end];
36
int
index = start - 1;
37
38
for
(
int
j = start; j < end; j++) {
39
if
(arr[j] <= pivot) {
40
std::swap(arr[++index], arr[j]);
41
}
42
}
43
44
std::swap(arr[index + 1], arr[end]);
45
return
index + 1;
46
}
47
58
void
iterativeQuickSort
(std::vector<int> &arr)
59
{
60
std::stack<int>
stack
;
61
int
start = 0;
62
int
end = arr.size()-1;
63
stack
.
push
(start);
64
stack
.
push
(end);
65
66
while
(!
stack
.empty())
67
{
68
end =
stack
.
top
();
69
stack
.
pop
();
70
start =
stack
.
top
();
71
stack
.
pop
();
72
73
int
pivotIndex =
partition
(arr,start,end);
74
75
if
(pivotIndex -1 > start)
76
{
77
stack
.
push
(start);
78
stack
.
push
(pivotIndex-1);
79
}
80
81
if
(pivotIndex+1<end)
82
{
83
stack
.
push
(pivotIndex+1);
84
stack
.
push
(end);
85
}
86
}
87
}
88
89
}
// namespace sorting
94
void
tests
()
95
{
96
//TEST 1 - Positive numbers
97
std::vector<int> case1={100,534,1000000,553,10,61,2000,238,2756,9,12,56,30};
98
std::cout<<
"TEST 1\n"
;
99
std::cout<<
"Before: \n"
;
100
for
(
auto
x : case1) std::cout<<x<<
","
;
101
std::cout<<
"\n"
;
102
sorting::iterativeQuickSort
(case1);
103
assert(std::is_sorted(std::begin(case1),std::end(case1)));
104
std::cout<<
"Test 1 succesful!\n"
;
105
std::cout<<
"After: \n"
;
106
for
(
auto
x : case1) std::cout<<x<<
","
;
107
std::cout<<
"\n"
;
108
109
//TEST 2 - Negative numbers
110
std::vector<int> case2={-10,-2,-5,-2,-3746,-785,-123, -452, -32456};
111
std::cout<<
"TEST 2\n"
;
112
std::cout<<
"Before: \n"
;
113
for
(
auto
x : case2) std::cout<<x<<
","
;
114
std::cout<<
"\n"
;
115
sorting::iterativeQuickSort
(case2);
116
assert(std::is_sorted(std::begin(case2),std::end(case2)));
117
std::cout<<
"Test 2 succesful!\n"
;
118
std::cout<<
"After: \n"
;
119
for
(
auto
x : case2) std::cout<<x<<
","
;
120
std::cout<<
"\n"
;
121
}
122
123
128
int
main
()
129
{
130
tests
();
// run self test implementation
131
return
0;
132
}
stack
for std::invalid_argument
Definition
stack.hpp:19
stack::pop
void pop()
Definition
stack.hpp:62
stack::push
void push(const value_type &item)
Definition
stack.hpp:47
stack::top
value_type top() const
Definition
stack.hpp:56
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
sorting
for working with vectors
sorting::iterativeQuickSort
void iterativeQuickSort(std::vector< int > &arr)
The main sorting function.
Definition
quick_sort_iterative.cpp:58
sorting::partition
int partition(std::vector< int > &arr, int start, int end)
The partition function sorts the array from start to end and uses the last element as the pivot.
Definition
quick_sort_iterative.cpp:33
tests
Testcases to check Union of Two Arrays.
sorting
quick_sort_iterative.cpp
Generated by
1.18.0