-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlb-19-sort-stack.cpp
More file actions
29 lines (29 loc) · 1.06 KB
/
Copy pathlb-19-sort-stack.cpp
File metadata and controls
29 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void SortedStack ::sort() {
stack<int> monotonically_decreasing;
// create monotonically decreasing stack
while (s.size()) {
int current_element = s.top();
s.pop();
if (monotonically_decreasing.size() == 0)
monotonically_decreasing.push(current_element);
else if (monotonically_decreasing.top() > current_element)
monotonically_decreasing.push(current_element);
else {
stack<int> temp;
while (monotonically_decreasing.size() and monotonically_decreasing.top() < current_element) {
temp.push(monotonically_decreasing.top());
monotonically_decreasing.pop();
}
monotonically_decreasing.push(current_element);
while (temp.size()) {
monotonically_decreasing.push(temp.top());
temp.pop();
}
}
}
// change the content of stack
while (monotonically_decreasing.size()) {
s.push(monotonically_decreasing.top());
monotonically_decreasing.pop();
}
}