Advertisement
Guest User

sortare_stiva

a guest
Nov 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. void insereaza(std::stack<int> &st, const int elem) {
  5.     if (st.empty()) {
  6.         st.push(elem);
  7.         return;
  8.     }
  9.     int top = st.top();
  10.     if (elem > top) {
  11.         st.push(elem);
  12.         return ;
  13.     }
  14.     st.pop();
  15.     insereaza(st, elem);
  16.     st.push(top);
  17. }
  18.  
  19. void sorteaza(std::stack<int> &st) {
  20.     if (st.empty()) {
  21.         return;
  22.     }
  23.     int top = st.top();
  24.     st.pop();
  25.     sorteaza(st);
  26.     insereaza(st, top);
  27. }
  28.  
  29. void afisare(std::stack<int> &st) {
  30.     if (st.empty())
  31.         return;
  32.     int top = st.top();
  33.     st.pop();
  34.     afisare(st);
  35.     std::cout << top << " ";
  36.     st.push(top);
  37. }
  38.  
  39. int main() {
  40.     int n;
  41.     std::cin >> n;
  42.     std::stack<int> st;
  43.     for (int i = 0; i < n; i++) {
  44.         int x;
  45.         std::cin >> x;
  46.         st.push(x);
  47.     }
  48.     sorteaza(st);
  49.     afisare(st);
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement