-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSorter.pde
More file actions
39 lines (37 loc) · 860 Bytes
/
BubbleSorter.pde
File metadata and controls
39 lines (37 loc) · 860 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
import java.util.List;
import java.util.LinkedList;
class BubbleSorter implements Sorter {
/**
for i = 1:n,
swapped = false
for j = n:i+1,
if a[j] < a[j-1],
swap a[j,j-1]
swapped = true
→ invariant: a[1..i] in final position
break if not swapped
end
**/
public List<Instruction> plan(int[] items) {
List<Instruction> steps = new LinkedList<Instruction>();
for(int i = 0; i < items.length; i++) {
boolean swapped = false;
for(int j = items.length-1; j > i; j--) {
if(items[j] < items[j-1]) {
int tmp = items[j];
items[j] = items[j-1];
items[j-1] = tmp;
swapped = true;
steps.add(new InstructionImpl(j-1, j, true, null));
}
else {
steps.add(new InstructionImpl(j-1, j, false, null));
}
}
if(!swapped) {
break;
}
}
return steps;
}
}