I am trying to write a code for sorting a stack without using extra space. It is logically correct but my output is completely random. Could someone point out my mistake?
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);
}
}