The title is the integer stored in the stack, and a sort of it. Alas, I didn't write it ....
Import Java.util.stack;public class Stack sort {public static void main (string[] args) {stack<integer> s = new Stack<int Eger> (); S.push (4); S.push (6); S.push (1); S.push (3); S.push (2);//while (!s.isempty ()) {//system.out.println ("before sorting") ;//system.out.println (S.pop ());//}s = sort (s), while (!s.isempty ()) {//system.out.println ("after sorting"); System.out.println (S.pop ());}} public static stack<integer> sort (stack<integer> s) {stack<integer> ret = new stack<integer> (); while (!s.isempty ()) {int top = S.pop (), while (!ret.isempty () && Ret.peek () < top) {S.push (Ret.pop ());} Ret.push (top);} return ret;}}
When the top of the auxiliary stack is smaller than the top of the original stack, the auxiliary stack element bounces back to the original stack at once until the top of the auxiliary stack is larger than the top element.
Stack sort, can only use extra one stack space && Youdao side