Sort the elements in a stack - Java

public class Sort {
    public Stack sort(Stack s){
        Stack sorted = new Stack();
        while (!s.isEmpty()) {
            int tmp = s.pop();
            while(!sorted.isEmpty() && sorted.pop() > tmp){
                s.push(sorted.pop());
            }
            sorted.push(tmp);
            System.out.println(sorted.TOP);
        }
        return sorted;
    }
}

Comments

Popular posts from this blog

public vs protected vs default access modifiers - Java

Class, Reference and Object