-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
SortStack.java
35 lines (33 loc) · 1.11 KB
/
SortStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package problems.medium;
import java.util.Stack;
/**
* Created by sherxon on 5/1/17.
*/
/**
* Sort stack elements so that top element is smallest in the stack,using only additional stack.
* */
public class SortStack {
/**
* pop element from stack and push it to helper stack and continue this as long as helper peek is smaller than
* stack pop. when top element in stakc is smaller than peek element in helper, move all elements
* from helper into stack and push poped stack element into helper.
* */
static void sort(Stack<Integer> stack){
if(stack.size()<=1)return;
Stack<Integer> helper=new Stack<>();
Integer pop=stack.pop();
helper.push(pop);
while (!stack.isEmpty()){
if(stack.peek() < helper.peek()){
pop=stack.pop();
while (!helper.isEmpty() && helper.peek()>pop){
stack.push(helper.pop());
}
helper.push(pop);
}else{
helper.push(stack.pop());
}
}
while (!helper.isEmpty())stack.push(helper.pop());
}
}