
#include <bits/stdc++.h>
#include <iostream>
using namespace std;


void insertatsortstack(int element, stack<int> &s){

    if(s.empty()==1 || element > s.top())
    {
        s.push(element);
        return;
    }

    int temp=s.top();
    s.pop();
    insertatsortstack(element,s);
    s.push(temp);
}

void sortstack(stack<int> &s){
    if(s.size()>0){
        int element=s.top();
        s.pop();
        sortstack(s);
        insertatsortstack(element,s);
    }
}

int main() {
	// your code goes here
	stack<int> s;
	s.push(10);
	s.push(8);
	s.push(6);
	s.push(4);
	s.push(2);
	
	while(s.size()>0) 
	{
		cout<<" "<<s.top()<<" ";
		s.pop();
	}
	cout<< endl;
	s.push(10);
	s.push(8);
	s.push(6);
	s.push(4);
	s.push(2);
	
	sortstack(s);
	while(s.size()>0) 
	{
		cout<<" "<<s.top()<<" ";
		s.pop();
	}
}

