/**
 * Company
 * Copyright (C) 2014-2017 All Rights Reserved.
 */
package com.cwenao.trying.stackandqueue;

/**
 * using two recursion to sort the stack, the method like reverse stack
 * @author cwenao
 * @version $Id SortStackWithRecursion.java, v 0.1 2017-10-07 23:55 cwenao Exp $$
 */
public class SortStackWithRecursion {
}

@@ -102,16 +102,41 @@
 queue;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * u
@@ -345,10 +345,967 @@
 rsion %7B%0A
+%0A%0A    public static void sortStack(Stack%3CInteger%3E stack) %7B%0A        if (!stack.isEmpty()) %7B%0A            int x = stack.pop();%0A            sortStack(stack);%0A            sortedInsert(stack, x);%0A        %7D%0A    %7D%0A%0A    private static void sortedInsert(Stack%3CInteger%3E stack, int x) %7B%0A%0A        if (stack.isEmpty() %7C%7C x %3E stack.peek()) %7B%0A            stack.push(x);%0A            return;%0A        %7D%0A        int tmp = stack.peek();%0A        stack.pop();%0A        sortedInsert(stack, x);%0A        stack.push(tmp);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        stack.push(1);%0A        stack.push(30);%0A        stack.push(20);%0A        stack.push(5);%0A        stack.push(100);%0A        stack.push(120);%0A        stack.push(-1);%0A        System.out.print(%22before Sorting: %22);%0A        System.out.println(stack);%0A%0A        sortStack(stack);%0A%0A        System.out.print(%22after Sorted: %22);%0A        System.out.println(stack);%0A%0A%0A    %7D%0A%0A
 %7D%0A

update the sort stack