@@ -0,0 +1,1807 @@
+package com.ttaylorr.ctci.answers.stacks;%0A%0Aimport com.ttaylorr.ctci.meta.Solution;%0A%0Aimport java.util.Stack;%0A%0A@Solution(chapter = 3, problem = 6)%0Apublic class StackSorter %7B%0A    protected final Stack%3CInteger%3E unsorted;%0A%0A    public StackSorter(Stack%3CInteger%3E unsorted) %7B%0A        this.unsorted = unsorted;%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public Stack%3CInteger%3E sort() %7B%0A        // We create a clone of the original stack so as not to mutate what we have as%0A        // a member of the class.%0A        //%0A        // We also create a new, empty Stack of the same type which will be filled%0A        // in order with the sorted elements.%0A        Stack%3CInteger%3E unsorted = (Stack%3CInteger%3E) this.unsorted.clone();%0A        Stack%3CInteger%3E sorted = new Stack%3C%3E();%0A%0A        // As we go, we'll remove elements from unsorted, marking them as sorted by their%0A        // nature of not being in the old list.  While there are still elements to be%0A        // sorted, this process should continue.%0A        while (!unsorted.isEmpty()) %7B%0A            // We pop the next unsorted element, and store it off in a local.%0A            int temp = unsorted.pop();%0A%0A            // We then move all of the elements that we can that are not sorted into the sorted%0A            // list that come before the element that we are trying to insert (see above)%0A            while (!sorted.isEmpty() && sorted.peek() %3E temp) %7B%0A                sorted.push(unsorted.pop());%0A            %7D%0A%0A            // Once that is complete, then we will push the element that we had stored off%0A            // in the local (see above comment for further explanation).%0A            sorted.push(temp);%0A        %7D%0A%0A        // Finally, once we're here, everything has been sorted (again, see above) and we are%0A        // good to go.%0A        return sorted;%0A    %7D%0A%7D%0A

Add StackSorter problem