-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeepLowestScored.java
More file actions
40 lines (33 loc) · 1.01 KB
/
KeepLowestScored.java
File metadata and controls
40 lines (33 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
// KeepLowestScored.java
import java.util.Arrays;
public class KeepLowestScored {
private String[] values;
private double[] scores;
public KeepLowestScored(int howMany) {
values = new String[howMany + 1];
scores = new double[howMany + 1];
for(int i = 0; i < howMany + 1; ++i) {
scores[i] = Double.MAX_VALUE;
}
}
public void insert(String s, double score) {
values[values.length - 1] = s;
scores[scores.length - 1] = score;
bubbleMinUp(values, scores);
}
private static void bubbleMinUp(String[] s, double[] d) {
for(int i = s.length - 1; i > 0; --i)
if(d[i] < d[i - 1]) {
// swap
double t = d[i];
d[i] = d[i - 1];
d[i - 1] = t;
String ts = s[i];
s[i] = s[i - 1];
s[i - 1] = ts;
}
}
public String[] lowestScored() {
return Arrays.copyOf(values, values.length - 1);
}
}