-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSortanStack.cpp
More file actions
54 lines (45 loc) · 744 Bytes
/
Copy pathSortanStack.cpp
File metadata and controls
54 lines (45 loc) · 744 Bytes
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
void insert(stack<int>& st,int temp)
{
if(st.size()==0 || st.top()<=temp)
{
st.push(temp);
return;
}
int val = st.top();
st.pop();
insert(st,temp);
st.push(val);
return;
}
void sort(stack<int>& st)
{
if(st.size()==1)
{
return;
}
int temp = st.top();
st.pop();
sort(st);
insert(st,temp);
return;
}
int main() {
stack<int> st;
st.push(2);
st.push(1);
st.push(0);
st.push(5);
sort(st);
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
st.pop();
cout<<st.top();
return 0;
}