-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStack.java
More file actions
29 lines (27 loc) · 750 Bytes
/
Copy pathSortStack.java
File metadata and controls
29 lines (27 loc) · 750 Bytes
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
import java.util.Stack;
public class SortStack {
static Stack<Integer> sort(Stack<Integer> s) {
Stack<Integer> retStack = new Stack<>();
while(!s.empty()) {
int temp = s.pop();
while(!retStack.empty() && temp < retStack.peek()) {
s.push(retStack.pop());
}
retStack.push(temp);
}
return retStack;
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(4);
s.push(67);
s.push(82);
s.push(32);
s.push(46);
Stack<Integer> sortedStack = sort(s);
System.out.println(sortedStack.toString());
// while(!sortedStack.empty()) {
//
// }
}
}