/* 3.6 Write a program to sort a stack in ascending order. YOu should not make
 * any assumptions about how the stack is implemented. The following are the 
 * only functions that should be used to write this program:
 * push | pop | peek | isEmpty
 * */

import java.util.Stack;

public class P0306 {
   public static Stack<Integer> sort(Stack<Integer> stack) {
      Stack<Integer> buffer = new Stack<Integer>();
      int tmp;

      while (!stack.isEmpty()) {
         if (buffer.isEmpty()) buffer.push(stack.pop());

         tmp = stack.pop();
         while (tmp >= buffer.peek() && !buffer.isEmpty()) {
            stack.push(buffer.pop());
         }
         buffer.push(tmp);
      }

      return buffer;
   }

   public static void main(String[] args) {
      Stack<Integer> stack = new Stack<Integer>();
      int[] data = {5, 1, 4, 3, 6};
      for (int i = 0; i < data.length; i++) {
         stack.push(data[i]);
      }
      stack = sort(stack);

      while (!stack.isEmpty()) {
         System.out.print(stack.pop() + " ");
      }
   }
}

@@ -470,140 +470,82 @@
     
-if (buffer.isEmpty()) buffer.push(stack.pop());%0A%0A         tmp = stack.pop();%0A         while (tmp %3E= buffer.peek() && !buffer.isEmpty
+tmp = stack.pop();%0A         while (!buffer.isEmpty() && tmp %3E= buffer.peek
 ()) 

make code more elegant