snippetjavaMinor
Sort stack in ascending order
Viewed 0 times
sortascendingstackorder
Problem
I am using two more helper stacks to sort. Kindly suggest improvements in terms of space and time.
import java.util.ArrayDeque;
import java.util.Deque;
public class SortStackAscendingOrder {
Deque stackhelper1 = new ArrayDeque<>();
Deque stackHelper2 = new ArrayDeque<>();
public Deque sortStack(Deque mainStack) {
if (mainStack.isEmpty()) {
return null;
}
while (!mainStack.isEmpty()) {
int mainItem = mainStack.pop();
int helperItem1 = -1;
if (!stackhelper1.isEmpty()) {
helperItem1 = stackhelper1.peek();
}
if (mainItem > helperItem1) {
// push all helperItem1 to helperItem2, till it is in ascending
// order
while (!stackhelper1.isEmpty() && mainItem > helperItem1) {
stackHelper2.push(stackhelper1.pop());
helperItem1 = stackhelper1.peek();
}
// push mainItem.
stackhelper1.push(mainItem);
// bring back all the helperItem2
while (!stackHelper2.isEmpty()) {
stackhelper1.push(stackHelper2.pop());
}
} else {
// directly push in stackhelper1
stackhelper1.push(mainItem);
}
}
return stackhelper1;
}
}Solution
Possible bug:
The following demo will throw a
Most likely the problem above is due to the fact that you return
API design:
First of all, I would consider implementing the stack sort as a static method that does not "leak" any internals. The idea here is that you sort the input stack: just like sorting methods in
All in all, I had this in mind:
The following demo will throw a
NullPointerException:public static void main(String[] args) {
Deque stack = new ArrayDeque<>();
stack.addLast(2);
stack.addLast(-1);
stack.addLast(-3);
stack.addLast(1);
System.out.println("Original stack: " + stack);
SortStackAscendingOrder sorter = new SortStackAscendingOrder();
Deque sortedStack = sorter.sortStack(stack);
System.out.println("Sorted original stack: " + sortedStack);
stack.clear();
stack.addLast(11);
stack.addLast(8);
stack.addLast(9);
stack.addLast(10);
System.out.println("Another stack: " +
sorter.sortStack(stack));
System.out.println("Sorted original stack again: " + sortedStack);
}Most likely the problem above is due to the fact that you return
stackhelper1, which is an essential internal facility in your algorithm.API design:
First of all, I would consider implementing the stack sort as a static method that does not "leak" any internals. The idea here is that you sort the input stack: just like sorting methods in
java.util.Arrays, all are void and modify the input arrays instead of returning a sorted copy.All in all, I had this in mind:
public static void sortStack(Deque mainStack) {
Deque stackHelper1 = new ArrayDeque<>();
Deque stackHelper2 = new ArrayDeque<>();
while (!mainStack.isEmpty()) {
int mainItem = mainStack.pop();
int helperItem1 = -1;
if (!stackHelper1.isEmpty()) {
helperItem1 = stackHelper1.peek();
}
if (mainItem > helperItem1) {
while (!stackHelper1.isEmpty() && mainItem > helperItem1) {
stackHelper2.push(stackHelper1.pop());
helperItem1 = stackHelper1.peek();
}
stackHelper1.push(mainItem);
while (!stackHelper2.isEmpty()) {
stackHelper1.push(stackHelper2.pop());
}
} else {
stackHelper1.push(mainItem);
}
}
mainStack.clear();
mainStack.addAll(stackHelper1);
}Code Snippets
public static void main(String[] args) {
Deque<Integer> stack = new ArrayDeque<>();
stack.addLast(2);
stack.addLast(-1);
stack.addLast(-3);
stack.addLast(1);
System.out.println("Original stack: " + stack);
SortStackAscendingOrder sorter = new SortStackAscendingOrder();
Deque<Integer> sortedStack = sorter.sortStack(stack);
System.out.println("Sorted original stack: " + sortedStack);
stack.clear();
stack.addLast(11);
stack.addLast(8);
stack.addLast(9);
stack.addLast(10);
System.out.println("Another stack: " +
sorter.sortStack(stack));
System.out.println("Sorted original stack again: " + sortedStack);
}public static void sortStack(Deque<Integer> mainStack) {
Deque<Integer> stackHelper1 = new ArrayDeque<>();
Deque<Integer> stackHelper2 = new ArrayDeque<>();
while (!mainStack.isEmpty()) {
int mainItem = mainStack.pop();
int helperItem1 = -1;
if (!stackHelper1.isEmpty()) {
helperItem1 = stackHelper1.peek();
}
if (mainItem > helperItem1) {
while (!stackHelper1.isEmpty() && mainItem > helperItem1) {
stackHelper2.push(stackHelper1.pop());
helperItem1 = stackHelper1.peek();
}
stackHelper1.push(mainItem);
while (!stackHelper2.isEmpty()) {
stackHelper1.push(stackHelper2.pop());
}
} else {
stackHelper1.push(mainItem);
}
}
mainStack.clear();
mainStack.addAll(stackHelper1);
}Context
StackExchange Code Review Q#120842, answer score: 3
Revisions (0)
No revisions yet.