-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackSorting.java
37 lines (31 loc) · 1.16 KB
/
StackSorting.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
36
37
package main.java.com.monotonic.testing.m2;
import java.util.Stack;
public class StackSorting {
public static Stack<Integer> sortStack(Stack<Integer> input){
Stack<Integer> tmpStack = new Stack<Integer>();
System.out.println("=============== debug logs ================");
while(!input.isEmpty()) {
int tmp = input.pop();
System.out.println("Element taken out: "+tmp);
while(!tmpStack.isEmpty() && tmpStack.peek() > tmp) {
input.push(tmpStack.pop());
}
tmpStack.push(tmp);
System.out.println("input: "+input);
System.out.println("tmpStack: "+tmpStack);
}
System.out.println("=============== debug logs ended ================");
return tmpStack;
}
public static void main(String a[]){
Stack<Integer> input = new Stack<Integer>();
input.add(34);
input.add(3);
input.add(31);
input.add(98);
input.add(92);
input.add(23);
System.out.println("input: "+input);
System.out.println("final sorted list: "+sortStack(input));
}
}