Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- void insereaza(std::stack<int> &st, const int elem) {
- if (st.empty()) {
- st.push(elem);
- return;
- }
- int top = st.top();
- if (elem > top) {
- st.push(elem);
- return ;
- }
- st.pop();
- insereaza(st, elem);
- st.push(top);
- }
- void sorteaza(std::stack<int> &st) {
- if (st.empty()) {
- return;
- }
- int top = st.top();
- st.pop();
- sorteaza(st);
- insereaza(st, top);
- }
- void afisare(std::stack<int> &st) {
- if (st.empty())
- return;
- int top = st.top();
- st.pop();
- afisare(st);
- std::cout << top << " ";
- st.push(top);
- }
- int main() {
- int n;
- std::cin >> n;
- std::stack<int> st;
- for (int i = 0; i < n; i++) {
- int x;
- std::cin >> x;
- st.push(x);
- }
- sorteaza(st);
- afisare(st);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement