-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStack.java
More file actions
77 lines (69 loc) · 2.12 KB
/
SortStack.java
File metadata and controls
77 lines (69 loc) · 2.12 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package datastructure.stackandqueue.stack;
import java.util.Iterator;
import java.util.Stack;
/**
* The type Sort stack.
*/
public class SortStack {
/**
* The Stack.
*/
Stack<Integer> stack;
/**
* Instantiates a new Sort stack.
*/
public SortStack(){
stack=new Stack<Integer>();
}
/**
* Sort with temporary stack.
*/
//1. Use a second tempStack.
//2. Pop value from mainStack.
//3. If the value is greater or equal to the top of tempStack, then push the value in tempStack
//else pop all values from tempStack and push them in mainStack and in the end push value in tempStack and repeat from step 2.
//till mainStack is not empty.
//4. When mainStack will be empty, tempStack will have sorted values in descending order.
//5. Now transfer values from tempStack to mainStack to make values sorted in ascending order.
public void sortWithTemporaryStack(){
Stack<Integer> tempStack = new Stack<Integer>();
while (!stack.isEmpty()){
Integer value = stack.pop();
if (!tempStack.isEmpty() && value >= tempStack.peek()) {
tempStack.push(value);
} else {
while (!tempStack.isEmpty() && tempStack.peek() > value)
stack.push(tempStack.pop());
tempStack.push(value);
}
}
while (!tempStack.isEmpty())
stack.push(tempStack.pop());
}
/**
* Print stack.
*/
public void printStack(){
Iterator<Integer> it = stack.iterator();
System.out.print("\nStack :: ");
while(it.hasNext()){
System.out.print(it.next()+" ");
}
}
/**
* Main.
*
* @param args the args
*/
public static void main(String[] args){
SortStack sortStack = new SortStack();
sortStack.stack.push(10);
sortStack.stack.push(20);
sortStack.stack.push(15);
sortStack.stack.push(40);
sortStack.stack.push(30);
sortStack.printStack();
sortStack.sortWithTemporaryStack();
sortStack.printStack();
}
}