Sort a stack | Practice | GeeksforGeeks

Given a stack, the task is to sort it such that the top of the stack has the greatest element.
Example 1:
Input:
Stack: 3 2 1
Output: 3 2 1

Example 2:
Input:
Stack: 11 2 32 3 41
Output: 41 32 11 3 2
Your Task: 
You don't have to read input or print anything. Your task is to complete the function sort() which sorts the elements present in the given stack. (The sorted stack is printed by the driver's code by popping the elements of the stack.)
Expected Time Complexity: O(N*N)
Expected Auxilliary Space: O(N) recursive.
Constraints:
1<=N<=100

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
rajanithinbatchu5 hours ago
class Solution:
    def sorted(self, s):
        s.sort()
        return s[::-1]
0
imabhishek0219 hours ago
Most easy way using Min Heap and push and pop.
void SortedStack :: sort()
{
priority_queue<int,vector<int>,greater<int>>pq;
 
 while(!s.empty())
 {
     pq.push(s.top());
     s.pop();
 }
 while(!pq.empty())
 {
     s.push(pq.top());
     pq.pop();
 }
}
+1
dipanshusharma93131 week ago
// java solution
class GfG{public Stack<Integer> sort(Stack<Integer> s){ //add code here. if(s.isEmpty()){     return s; } int temp = s.pop(); sort(s); return insertstack(s,temp);}   public Stack<Integer> insertstack(Stack<Integer> s , int item){      if(s.isEmpty()){       s.push(item);       return s;   }   if(item < s.peek()){       int temp = s.pop();       insertstack(s, item);       s.push(temp);   }   else{       s.push(item);       return s;   }   return s;}}
0
shilsoumyadip2 weeks ago
void solve(stack<int>& st){  vector<int>v;  while(!st.empty()){      v.push_back(st.top());      st.pop();  }  sort(v.begin(),v.end());  for(int i:v){      st.push(i);  }}void SortedStack :: sort(){  //Your code here  solve(s);  }
+1
oyesaurabh2 weeks ago
void insert(stack<int> &s,int e){
    if(s.empty() || s.top()<e){
        s.push(e);
        return;
    }
    int t=s.top();
    s.pop();
    insert(s,e);
    s.push(t);
}
void SortedStack :: sort()
{
   //Your code here
   if(s.empty())return;
    int t=s.top();
    s.pop();
    sort();
    insert(s,t);
}

0
hharshit81182 weeks ago
using two stacks :: 
void SortedStack :: sort(){   stack<int> s2;   while(!s.empty()){       int t = s.top();       s.pop();       while(s2.empty() == false && s2.top() > t){           s.push(s2.top());           s2.pop();       }       s2.push(t);   }   s = s2;}
0
harshscode2 weeks ago
 vector<int> v;  while(!s.empty())  {      v.push_back(s.top());      s.pop();  }  sort(v.begin(),v.end());  for(auto i:v)  s.push(i);
0
kaushalkumardbg622 weeks ago
void go(stack<int>& s){   vector<int>v;   while(!s.empty()){       v.push_back(s.top());       s.pop();   }   sort(v.begin(),v.end());   for(int i:v){       s.push(i);   }}
void SortedStack :: sort(){  //Your code here  go(s);   }
+1
iamnobodyji2 weeks ago
Simple java solution: 
class GfG{
	public Stack<Integer> sort(Stack<Integer> s)
	{
		//add code here.
		Stack<Integer> t = new Stack<>();
		t.push(s.pop());
		while(!s.empty()){
		    if(s.peek()<t.peek()){
                int t1 = s.pop();
                int t2=t.peek();
                while(t2>t1 && !t.empty()){
                    s.push(t2);
                    t.pop();
                    if(!t.empty()){
                        t2=t.peek();
                    }
                }
                t.push(t1);
		    }else{
		        t.push(s.pop());
		    }
		}
		
		return t;
	}
}
0
abhitiwari9876at
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.