Stack Sort
Prompt:
Sort a Stack class in ascending order without using any other data structure other than one more Stack. Solve it in strictly linear space complexity. You have access to the push, pop and isEmpty methods on the Stack.
Go!
First off, what is a Stack? To refresh your memory, a Stack resembles dirty dishes in a sink. When you need to wash them, you start with the dishes on top rather than the dishes on the bottom. So, the first dirty dish to enter the sink gets washed last.
There are three important methods on Stack:
Push(value)
: Adds the value to the top of the StackPop():
Removes the value at the top of the Stack, and returns the valueisEmpty():
Returns whether the Stack has any items
Setup
Inputs
: An unsorted Stack with some integersOutputs
: A sorted Stack (largest integers on top, smallest at bottom)Constraints
: Only use another Stack to help sort, and variables when required
My method involves popping a value from one Stack, comparing it to a variable, and selectively pushing it to another Stack. When the first Stack is empty, we can run the same Algorithmthm on the second Stack and selectively push values to the first Stack. When there are no more items to push, we can successfully say that we have sorted the Stack.
If you did not understand the solution from the paragraph above, do not fear, the rest of this article is for you!
Solution
Let us use an example problem to illustrate this technique.
Stack A | Stack B (empty) |
---|---|
9 | |
4 | |
12 | |
6 | |
7 |
Also, we have three variables, i
, that starts at 0
, comparison
, that starts at undefined
, and done
that starts as false
.
Start: Full stack - Stack A, while empty stack is Stack B.
- Set done to be true at the beginning of every loop.
pop()
on Stack A. This returns 9. Stack now has only 4 elements, since 9 is removed from the Stack.- Assign 9 to comparison.
- If
i
is divisible by 2:- Compare 4 to
comparison
(9), and push the smaller value in the other Stack (Stack B). - We place the larger value in the comparison variable.
- Compare 4 to
- If
i
is not divisible by 2:- Compare 4 to
comparison
(9), and push the larger value in the other Stack (Stack B). - We place the smaller value in the
comparison
variable.
- Compare 4 to
- Repeat process over the entire Stack A. If the
comparison
variable changes at any time, set done to be false - When Stack A is empty,
isEmpty(Stack A)
returns true. i
is incremented. In this case,i
is 1.- If done is true, we push comparison value to the recently filled Stack. For
i % 2 = 0
, it is Stack B, while fori % 2 = 1
, it is Stack A. - If
i
is not divisible by 2:- Repeat 1 through 7.
- Push value in
comparison
variable in Stack A. - Move items from Stack B to Stack A.
- If
i
is divisible by 2:- Repeat 1 through 7.
- Push value in
comparison
variable in Stack B. - Move items from Stack A to Stack B.
After the first pass, the stacks should look as follows:
Stack A (empty) | Stack B |
---|---|
- | |
7 | |
6 | |
9 | |
4 |
i = 1,
comparison = 12
After the second pass, the stacks should look as follows:
Stack A | Stack B (empty) |
---|---|
- | |
6 | |
9 | |
7 | |
12 |
i = 2,
comparison = 4
After the third pass, the stacks should look as follows:
Stack A (empty) | Stack B |
---|---|
- | |
9 | |
7 | |
6 | |
4 |
i = 3,
comparison = 12
After the fourth pass, the stacks should look as follows:
Stack A | Stack B (empty) |
---|---|
- | |
6 | |
7 | |
9 | |
12 |
i = 4,
comparison = 4
Since done stays as true through the entire pass, we push the value in comparison to Stack A.
Eventually, it looks like this:
Stack A | Stack B (empty) |
---|---|
4 | |
6 | |
7 | |
9 | |
12 |
And voila, sort for a Stack has been accomplished!
Have questions or suggestions? Feel free to open an issue on GitHub.
Cheers!