Skip to content

Commit 8fdad69

Browse files
committed
commit Exercise_30_19
1 parent 63ec2f1 commit 8fdad69

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

Chapter_30/exercise_30_19/Exercise_30_19.java

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,29 @@ public SelectionSortPane(ArrayList<Integer> list, Label lblSortType) {
8585

8686
@Override
8787
public void run() {
88-
for(int i = 0; i < list.length - 1; i++) {
89-
int currentMin = list[i];
90-
int currentMinIndex = i;
91-
for(int j = i + 1; j < list.length; j++) {
92-
if(currentMin > list[j]) {
93-
currentMin = list[j];
94-
currentMinIndex = j;
88+
try {
89+
for(int i = 0; i < list.length - 1; i++) {
90+
int currentMin = list[i];
91+
int currentMinIndex = i;
92+
for(int j = i + 1; j < list.length; j++) {
93+
if(currentMin > list[j]) {
94+
currentMin = list[j];
95+
currentMinIndex = j;
96+
}
9597
}
96-
}
97-
if(currentMinIndex != i) {
98-
list[currentMinIndex] = list[i];
99-
list[i] = currentMin;
100-
}
101-
int index = i;
102-
Platform.runLater(() -> repaintHistogram(index));
103-
try {
98+
if(currentMinIndex != i) {
99+
list[currentMinIndex] = list[i];
100+
list[i] = currentMin;
101+
}
102+
int index = i;
103+
Platform.runLater(() -> repaintHistogram(index));
104104
Thread.sleep(500);
105105
}
106-
catch (InterruptedException ex) {
107-
}
108106
}
107+
catch (InterruptedException ex) {
108+
ex.printStackTrace();
109+
}
110+
Platform.runLater(() -> repaintHistogram(list.length - 1));
109111
}
110112
}
111113

@@ -119,20 +121,21 @@ public InsertionSortPane(ArrayList<Integer> list, Label lblSortType) {
119121

120122
@Override
121123
public void run() {
122-
for(int i = 1; i < list.length; i++) {
123-
int currentElement = list[i];
124-
int k;
125-
for(k = i - 1; k >= 0 && list[k] > currentElement; k--) {
126-
list[k + 1] = list[k];
127-
}
128-
list[k + 1] = currentElement;
129-
int index = i;
130-
Platform.runLater(() -> repaintHistogram(index));
131-
try {
124+
try {
125+
for(int i = 1; i < list.length; i++) {
126+
int currentElement = list[i];
127+
int k;
128+
for(k = i - 1; k >= 0 && list[k] > currentElement; k--) {
129+
list[k + 1] = list[k];
130+
}
131+
list[k + 1] = currentElement;
132+
int index = i;
133+
Platform.runLater(() -> repaintHistogram(index));
132134
Thread.sleep(500);
133135
}
134-
catch (InterruptedException ex) {
135-
}
136+
}
137+
catch (InterruptedException ex) {
138+
ex.printStackTrace();
136139
}
137140
}
138141
}
@@ -148,24 +151,33 @@ public BubbleSortPane(ArrayList<Integer> list, Label lblSortType) {
148151
@Override
149152
public void run() {
150153
boolean needNextPass = true;
151-
for(int k = 1; k < list.length && needNextPass; k++) {
152-
needNextPass = false;
153-
for(int i = 0; i < list.length - k; i++) {
154-
if(list[i] > list[i + 1]) {
155-
Integer temp = list[i];
156-
list[i] = list[i + 1];
157-
list[i + 1] = temp;
158-
needNextPass = true;
154+
int l = 0;
155+
try {
156+
for(int k = 1; k < list.length && needNextPass; k++) {
157+
needNextPass = false;
158+
for(int i = 0; i < list.length - k; i++) {
159+
if(list[i] > list[i + 1]) {
160+
Integer temp = list[i];
161+
list[i] = list[i + 1];
162+
list[i + 1] = temp;
163+
needNextPass = true;
164+
}
159165
}
160-
}
161-
int index = list.length - k;
162-
Platform.runLater(() -> repaintHistogram(index));
163-
try {
166+
int index = list.length - k;
167+
l = index;
168+
Platform.runLater(() -> repaintHistogram(index));
164169
Thread.sleep(500);
165170
}
166-
catch (InterruptedException ex) {
171+
while(l >= 0) {
172+
int index = l;
173+
Platform.runLater(() -> repaintHistogram(index));
174+
l--;
175+
Thread.sleep(500);
167176
}
168177
}
178+
catch(InterruptedException ex) {
179+
ex.printStackTrace();
180+
}
169181
}
170182
}
171183

0 commit comments

Comments
 (0)