Skip to content

Commit

Permalink
Problem 05 - Ch 03
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak-malik committed Feb 4, 2017
1 parent 69453e7 commit 065cc45
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Java Solutions for Cracking the Coding Interview - 6th Edition
- [X] [02 - Stack Min](../master/src/com/deepak/ctci/Ch03_Stacks_And_Queues/Problem_02.java)
- [X] [03 - Stack of Plates](../master/src/com/deepak/ctci/Ch03_Stacks_And_Queues/Problem_03.java)
- [X] [04 - Queue via Stacks](../master/src/com/deepak/ctci/Ch03_Stacks_And_Queues/Problem_04.java)
- [ ] 05 - Sort Stack
- [X] [05 - Sort Stack](../master/src/com/deepak/ctci/Ch03_Stacks_And_Queues/Problem_05.java)
- [ ] 06 - Animal Shelter

**Ch 04 - Trees and Graphs**
Expand Down
30 changes: 30 additions & 0 deletions src/com/deepak/ctci/Ch03_Stacks_And_Queues/Problem_05.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.deepak.ctci.Ch03_Stacks_And_Queues;

import java.util.Stack;

/**
* <br> Problem Statement :
*
Expand All @@ -19,4 +21,32 @@
*/
public class Problem_05 {

/**
* Method to sort stack
*
* @param input
* @return {@link Stack}
*/
public static Stack<Integer> sortStack(Stack<Integer> input) {
/* If input is null, no processing needed */
if (input == null) {
return null;
}
/*Create a temp stack */
Stack<Integer> tempStack = new Stack<>();
/* Keep going until input is not empty */
while (!input.isEmpty()) {
/* Pop value from input */
int tempValue = input.pop();
/* We want smallest one at the bottom. So keep comparing and
* if temp stack has bigger item, pop it and push it to input stack */
while (!tempStack.isEmpty() && tempStack.peek() > tempValue) {
input.push(tempStack.pop());
}
/* Push temp value to the temp stack */
tempStack.push(tempValue);
}
return tempStack;
}

}
44 changes: 44 additions & 0 deletions test/com/deepak/ctci/Ch03_Stacks_And_Queues/Problem_05_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Cracking-The-Coding-Interview
* Problem_04_Test.java
*/
package com.deepak.ctci.Ch03_Stacks_And_Queues;

import java.util.Stack;

import org.junit.Assert;
import org.junit.Test;

/**
* Test cases for Problem 05
*
* @author Deepak
*/
public class Problem_05_Test {

/**
* Test cases for sorting a stack
*/
@Test
public void testSortStack() {
Stack<Integer> input = new Stack<>();
input.push(2);
input.push(9);
input.push(7);
input.push(3);
input.push(1);
input.push(5);
input.push(10);
Assert.assertTrue(input.size() == 7);
Stack<Integer> output = Problem_05.sortStack(input);
Assert.assertTrue(output.pop() == 10);
Assert.assertTrue(output.pop() == 9);
Assert.assertTrue(output.pop() == 7);
Assert.assertTrue(output.pop() == 5);
Assert.assertTrue(output.pop() == 3);
Assert.assertTrue(output.pop() == 2);
Assert.assertTrue(output.pop() == 1);
Assert.assertTrue(output.size() == 0);
}

}

0 comments on commit 065cc45

Please sign in to comment.