-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
69 lines (49 loc) · 1.31 KB
/
SelectionSort.java
File metadata and controls
69 lines (49 loc) · 1.31 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 SelectionSort implements Sortable {
private ArrayList<Button> array;
private JTextField a,b;
private JButton swap;
public SelectionSort(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 x = 0; x < array.size();x++) {
int min = x;
for(int y = x; y < array.size();y++) {
input(min,y);
if(check()) {
min = y;
}
}
input(min, x);
swap.doClick();
array.get(x).setState(Button.SECURED);
}
}
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/array.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}