-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
69 lines (47 loc) · 1.22 KB
/
InsertionSort.java
File metadata and controls
69 lines (47 loc) · 1.22 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JTextField;
public class InsertionSort implements Sortable {
private ArrayList<Button> array;
private JTextField a,b;
private JButton swap;
public InsertionSort(ArrayList<Button> array, JTextField a, JTextField b,JButton swap) {
this.array = array;
this.a = a;
this.b = b;
this.swap = swap;
sort();
}
public void sort() {
for(int o = 1; o < array.size();o++) {
for(int i = o; i > 0; i--) {
input(i-1,i);
if(check()) {
swap.doClick();
} else {
break;
}
}
}
}
public boolean check() {
if(APanel.getInt(array.get(APanel.getInt(a))) > APanel.getInt(array.get(APanel.getInt(b))) ) {
return true;
}
return false;
}
private void input(int a,int b) {
for(Button i: array) {
i.setState(Button.NONE);
}
this.a.setText("" + (a));
array.get(APanel.getInt(this.a)).setState(Button.COMPARE);
this.b.setText("" + b);
array.get(APanel.getInt(this.b)).setState(Button.COMPARE);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}