-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPQSorted.java
More file actions
50 lines (38 loc) · 800 Bytes
/
PQSorted.java
File metadata and controls
50 lines (38 loc) · 800 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class PQSorted implements PriorityQueue {
Integer[] data;
int numElts;
public int size() {
return numElts;
}
public boolean isEmpty() {
return numElts == 0;
}
public Integer remove() {
if(numElts == 0) return null;
numElts--;
Integer toReturn = data[numElts];
data[numElts] = null;
return toReturn;
}
public void add(Integer toAdd) {
if(numElts == data.length) resize();
int i = numElts - 1;
while(i >= 0 && data[i] > toAdd) {
data[i+1] = data[i];
i--;
}
data[i+1] = toAdd;
numElts++;
}
private void resize() {
Integer[] temp = new Integer[numElts * 2];
for(int i = 0; i < numElts; i++) {
temp[i] = data[i];
}
data = temp;
}
public PQSorted() {
data = new Integer[2];
numElts = 0;
}
}