forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStack.java
More file actions
65 lines (56 loc) · 1.84 KB
/
SortStack.java
File metadata and controls
65 lines (56 loc) · 1.84 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
package com.geeksforgeeks.stack;
import java.util.Stack;
public class SortStack {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(30);
stack.push(-5);
stack.push(18);
stack.push(14);
stack.push(-3);
System.out.println("Before Sorting" + stack);
sortStack(stack);
System.out.println("After Sorting" + stack);
Stack<Integer> input = new Stack<>();
input.push(34);
input.push(3);
input.push(31);
input.push(98);
input.push(92);
input.push(23);
sortStackUsingTempStack(input);
}
public static void sortStackUsingTempStack(Stack<Integer> stack) {
System.out.println("Before Sorting using temp stack " + stack);
Stack<Integer> tempStack = new Stack<>();
Integer popped = null;
while (!stack.isEmpty()) {
popped = stack.pop();
if (tempStack.isEmpty()) {
tempStack.push(popped);
} else {
while (!tempStack.isEmpty() && tempStack.peek() > popped) {
stack.push(tempStack.pop());
}
tempStack.push(popped);
}
}
System.out.println("After Sorting using temporary Stack " + tempStack);
}
public static void sortStack(Stack<Integer> stack) {
if (!stack.isEmpty()) {
Integer popped = stack.pop();
sortStack(stack);
insertAtBottom(stack, popped);
}
}
private static void insertAtBottom(Stack<Integer> stack, Integer item) {
if (stack.isEmpty() || stack.peek() < item) {
stack.push(item);
} else {
Integer popped = stack.pop();
insertAtBottom(stack, item);
stack.push(popped);
}
}
}