-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
72 lines (63 loc) 路 1.96 KB
/
Copy pathSelectionSort.java
File metadata and controls
72 lines (63 loc) 路 1.96 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
70
71
72
package Visualizer.Sorts;
import Visualizer.*;
public class SelectionSort implements Runnable{
private Integer[] toBeSorted;
private VisualizerFrame frame;
private boolean fast;
public SelectionSort(Integer[] toBeSorted, VisualizerFrame frame, boolean fast) {
this.toBeSorted = toBeSorted;
this.frame = frame;
this.fast = fast;
}
public void run() {
if (fast) {
sortFast();
} else {
sortSlow();
}
SortingVisualizer.isSorting=false;
}
public void sortFast(){
int temp = 0;
int selected = 0;
for(int i = 0; i<toBeSorted.length; i++){
selected = i;
for(int j = toBeSorted.length-1; j>i; j--){
if (toBeSorted[j] <= toBeSorted[selected]){
selected = j;
}
}
frame.reDrawArray(toBeSorted);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
temp = toBeSorted[i];
toBeSorted[i] = toBeSorted[selected];
toBeSorted[selected]= temp;
}
}
public void sortSlow() {
int temp = 0;
int selected = 0;
for(int i = 0; i<toBeSorted.length; i++){
selected = i;
for(int j = toBeSorted.length-1; j>i; j--){
if (toBeSorted[j] <= toBeSorted[selected]){
selected = j;
}
frame.reDrawArray(toBeSorted, selected, j-1);
try {
Thread.sleep(SortingVisualizer.sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
temp = toBeSorted[i];
toBeSorted[i] = toBeSorted[selected];
toBeSorted[selected]= temp;
}
frame.reDrawArray(toBeSorted);
}
}