Skip to content

Commit 63ec2f1

Browse files
committed
commit Exercise_30_19
1 parent f34c1ec commit 63ec2f1

File tree

1 file changed

+94
-21
lines changed

1 file changed

+94
-21
lines changed

Chapter_30/exercise_30_19/Exercise_30_19.java

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ public class Exercise_30_19 extends Application {
1818

1919
@Override
2020
public void start(Stage primaryStage) {
21-
ArrayList<Integer> list1 = new ArrayList<>();
21+
ArrayList<Integer> listOfElements = new ArrayList<>();
2222
for(int i = 0; i < 50; i++) {
23-
list1.add(i + 1);
23+
listOfElements.add(i + 1);
2424
}
25-
Collections.shuffle(list1);
25+
Collections.shuffle(listOfElements);
2626

2727
HBox hBox = new HBox(10);
2828
hBox.setPadding(new Insets(5));
2929

30-
HistogramPane pane1 = new SelectionSortPane(list1, new Label("Selection Sort"));
31-
// HistogramPane pane2 = new HistogramPane(list1, new Label("Insertion Sort"));
32-
// HistogramPane pane3 = new HistogramPane(list1, new Label("Bubble Sort"));
33-
hBox.getChildren().addAll(pane1);
30+
HistogramPane paneForSelectionSort = new SelectionSortPane(listOfElements, new Label("Selection Sort"));
31+
HistogramPane paneForInsertionSort = new InsertionSortPane(listOfElements, new Label("Insertion Sort"));
32+
HistogramPane paneForBubbleSort = new BubbleSortPane(listOfElements, new Label("Bubble Sort"));
33+
hBox.getChildren().addAll(paneForSelectionSort, paneForInsertionSort, paneForBubbleSort);
3434

3535
Scene scene = new Scene(hBox);
3636
scene.getStylesheets().add("file:///c:/programming/java/Intro_to_Java_Programming_10th_exercises/JavaProgrammingExercises/Chapter_30/exercise_30_19/style2.css");
@@ -40,24 +40,25 @@ public void start(Stage primaryStage) {
4040
}
4141

4242
private abstract class HistogramPane extends BorderPane {
43-
protected ArrayList<Integer> list;
43+
protected Integer[] list;
4444
protected HBox hBoxForChart = new HBox();
4545
protected Label lblSortType;
4646
protected Rectangle[] rectangles;
4747
protected final int WIDTH = 5;
4848
protected final int HEIGHT = 5;
4949

5050
public HistogramPane(ArrayList<Integer> list, Label lblSortType) {
51-
this.list = list;
51+
this.list = new Integer[list.size()];
52+
this.list = list.toArray(this.list);
5253
this.lblSortType = lblSortType;
5354
rectangles = new Rectangle[list.size()];
5455
setCenter(hBoxForChart);
5556
setTop(lblSortType);
5657
BorderPane.setAlignment(lblSortType, Pos.CENTER);
57-
repaintHistogram();
58+
repaintHistogram(-1);
5859
}
5960

60-
public void repaintHistogram() {
61+
public void repaintHistogram(int index) {
6162
hBoxForChart.getChildren().clear();
6263
hBoxForChart.setAlignment(Pos.BASELINE_CENTER);
6364
int j = 0;
@@ -68,34 +69,106 @@ public void repaintHistogram() {
6869
hBoxForChart.getChildren().add(rectangles[j]);
6970
j++;
7071
}
72+
if(index != -1) {
73+
rectangles[index].setFill(Color.BLUE);
74+
}
7175
}
7276
}
7377

74-
private class SelectionSortPane extends HistogramPane {
78+
private class SelectionSortPane extends HistogramPane implements Runnable {
7579

7680
public SelectionSortPane(ArrayList<Integer> list, Label lblSortType) {
7781
super(list, lblSortType);
82+
Thread thread = new Thread(this);
83+
thread.start();
7884
}
7985

80-
public void selectionSort() {
81-
for(int i = 0; i < list.size() - 1; i++) {
82-
int currentMin = list.get(i);
86+
@Override
87+
public void run() {
88+
for(int i = 0; i < list.length - 1; i++) {
89+
int currentMin = list[i];
8390
int currentMinIndex = i;
84-
for(int j = i + 1; j < list.size(); j++) {
85-
if(currentMin > list.get(j)) {
86-
currentMin = list.get(j);
91+
for(int j = i + 1; j < list.length; j++) {
92+
if(currentMin > list[j]) {
93+
currentMin = list[j];
8794
currentMinIndex = j;
8895
}
8996
}
9097
if(currentMinIndex != i) {
91-
list.set(currentMinIndex, list.get(i));
92-
list.set(i, currentMin);
98+
list[currentMinIndex] = list[i];
99+
list[i] = currentMin;
100+
}
101+
int index = i;
102+
Platform.runLater(() -> repaintHistogram(index));
103+
try {
104+
Thread.sleep(500);
105+
}
106+
catch (InterruptedException ex) {
107+
}
108+
}
109+
}
110+
}
111+
112+
private class InsertionSortPane extends HistogramPane implements Runnable {
113+
114+
public InsertionSortPane(ArrayList<Integer> list, Label lblSortType) {
115+
super(list, lblSortType);
116+
Thread thread = new Thread(this);
117+
thread.start();
118+
}
119+
120+
@Override
121+
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 {
132+
Thread.sleep(500);
133+
}
134+
catch (InterruptedException ex) {
93135
}
94-
repaintHistogram();
95136
}
96137
}
97138
}
139+
140+
private class BubbleSortPane extends HistogramPane implements Runnable {
98141

142+
public BubbleSortPane(ArrayList<Integer> list, Label lblSortType) {
143+
super(list, lblSortType);
144+
Thread thread = new Thread(this);
145+
thread.start();
146+
}
147+
148+
@Override
149+
public void run() {
150+
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;
159+
}
160+
}
161+
int index = list.length - k;
162+
Platform.runLater(() -> repaintHistogram(index));
163+
try {
164+
Thread.sleep(500);
165+
}
166+
catch (InterruptedException ex) {
167+
}
168+
}
169+
}
170+
}
171+
99172
public static void main(String[] args) {
100173
launch(args);
101174
}

0 commit comments

Comments
 (0)