xxxxxxxxxx
// Java program to sort a stack using
// a auxiliary stack.
import java.util.*;
class SortStack
{
// This function return the sorted stack
public static Stack<Integer> sortstack(Stack<Integer>
input)
{
Stack<Integer> tmpStack = new Stack<Integer>();
while(!input.isEmpty())
{
// pop out the first element
int tmp = input.pop();
// while temporary stack is not empty and
// top of stack is greater than temp
while(!tmpStack.isEmpty() && tmpStack.peek()
> tmp)
{
// pop from temporary stack and
// push it to the input stack
input.push(tmpStack.pop());
}
// push temp in temporary of stack
tmpStack.push(tmp);
}
return tmpStack;
}
// Driver Code
public static void main(String args[])
{
Stack<Integer> input = new Stack<Integer>();
input.add(34);
input.add(3);
input.add(31);
input.add(98);
input.add(92);
input.add(23);
// This is the temporary stack
Stack<Integer> tmpStack=sortstack(input);
System.out.println("Sorted numbers are:");
while (!tmpStack.empty())
{
System.out.print(tmpStack.pop()+" ");
}
}
}
// This code is contributed by Danish Kaleem
xxxxxxxxxx
public class Solution {
public ArrayList<Integer> solve(ArrayList<Integer> A) {
Stack<Integer> input_stack = new Stack<>();
input_stack.addAll(A);
Stack<Integer> sorted_stack = new Stack<>();
while(!input_stack.isEmpty()){
int top = input_stack.pop();
insertInStack(sorted_stack, top);
}
return new ArrayList<>(sorted_stack);
}
private void insertInStack(Stack<Integer> sorted_stack, int temp){
if(sorted_stack.isEmpty() || sorted_stack.peek() >= temp){
sorted_stack.push(temp);
return;
}
int top = sorted_stack.pop();
insertInStack(sorted_stack, temp);
sorted_stack.push(top);
}
}
JavaCopy