fork download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. stack<int> sortst(stack<int> input)
  5. {
  6. int poped;
  7. stack<int> temp;
  8. while (!input.empty())
  9. {
  10. poped = input.top();
  11. input.pop();
  12. while (!temp.empty() && temp.top() < poped)
  13. {
  14. int pop2 = temp.top();
  15. input.push(pop2);
  16. temp.pop();
  17. }
  18. temp.push(poped);
  19. }
  20. return temp;
  21. }
  22. int main() {
  23. stack<int> input;
  24. input.push(3);
  25. input.push(6);
  26. input.push(1);
  27. input.push(2);
  28. input.push(9);
  29. input = sortst(input);
  30. while (!input.empty())
  31. {
  32. cout << input.top() << ' ';
  33. input.pop();
  34. }
  35. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
1 2 3 6 9