-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq3p5.java
More file actions
43 lines (42 loc) · 1.01 KB
/
Copy pathq3p5.java
File metadata and controls
43 lines (42 loc) · 1.01 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
import java.util.*;
import java.lang.Integer;
class q3p5{
public static void main(String[] args){
Stack<Integer> sk = new Stack<>();
sk.push(2);
sk.push(5);
sk.push(1);
sk.push(4);
sort(sk);
System.out.println(sk.peek());
}
// T: O(n^2)
// S: O(n)
// A: other method: merge sort T:O(nlgn), S:O(nlgn)
//
// and quick sort: T:O(nlgn), S: O(n)
// note: if no stack size limited.
//
//
public static void sort(Stack<Integer> s){
Stack<Integer> bufStk = new Stack<>();
while(!s.empty()){
int minValue = s.pop();
while(!bufStk.empty() && (bufStk.peek()>minValue)){
s.push(bufStk.pop());
}
bufStk.push(minValue);
}
while(!bufStk.empty()){
s.push(bufStk.pop());
}
}
}
/*
class SortStack<Integer> extends Stack<Integer>{
public boolean sort(){
int aa = Integer.MAX_VALUE; // error here, relate to Stack<Integer>
return true;
}
}
*/