Skip to content

Commit 193276b

Browse files
committedSep 2, 2017
Chapter 04 section 11 codes updated.
1 parent fea4f4b commit 193276b

File tree

4 files changed

+41
-84
lines changed

4 files changed

+41
-84
lines changed
 

‎04-Sort-Visualization/11-Heap-Sort-Visualization/src/AlgoFrame.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import java.awt.Graphics2D;
2-
import java.awt.Graphics;
3-
import java.awt.Dimension;
4-
import java.awt.Color;
5-
import java.awt.RenderingHints;
6-
1+
import java.awt.*;
72
import javax.swing.*;
83

94
public class AlgoFrame extends JFrame{
105

116
private int canvasWidth;
127
private int canvasHeight;
13-
private JPanel canvas;
148

159
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1610

@@ -38,8 +32,8 @@ public AlgoFrame(String title){
3832
public int getCanvasHeight(){return canvasHeight;}
3933

4034
// data
41-
HeapSortData data;
42-
public void setData(HeapSortData data){
35+
private HeapSortData data;
36+
public void render(HeapSortData data){
4337
this.data = data;
4438
repaint();
4539
}
@@ -66,7 +60,6 @@ public void paintComponent(Graphics g) {
6660

6761
// 具体绘制
6862
int w = canvasWidth/data.N();
69-
//AlgoVisHelper.setColor(g2d, AlgoVisHelper.Grey);
7063
for(int i = 0 ; i < data.N() ; i ++ ) {
7164
if ( i >= data.heapIndex)
7265
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);

‎04-Sort-Visualization/11-Heap-Sort-Visualization/src/AlgoVisHelper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.awt.geom.Rectangle2D;
55
import java.lang.InterruptedException;
66

7-
87
public class AlgoVisHelper {
98

109
private AlgoVisHelper(){}
@@ -31,36 +30,35 @@ private AlgoVisHelper(){}
3130
public static final Color Black = new Color(0x000000);
3231
public static final Color White = new Color(0xFFFFFF);
3332

34-
35-
static public void strokeCircle(Graphics2D g, int x, int y, int r){
33+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
3634

3735
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
3836
g.draw(circle);
3937
}
4038

41-
static public void fillCircle(Graphics2D g, int x, int y, int r){
39+
public static void fillCircle(Graphics2D g, int x, int y, int r){
4240

4341
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4442
g.fill(circle);
4543
}
4644

47-
static public void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
45+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
4846

4947
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5048
g.draw(rectangle);
5149
}
5250

53-
static public void fillRectangle(Graphics2D g, int x, int y, int w, int h){
51+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5452

5553
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5654
g.fill(rectangle);
5755
}
5856

59-
static public void setColor(Graphics2D g, Color color){
57+
public static void setColor(Graphics2D g, Color color){
6058
g.setColor(color);
6159
}
6260

63-
static public void setStrokeWidth(Graphics2D g, int w){
61+
public static void setStrokeWidth(Graphics2D g, int w){
6462
int strokeWidth = w;
6563
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6664
}
Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
import java.awt.*;
2-
import java.util.Random;
3-
import java.util.Arrays;
2+
43

54
public class AlgoVisualizer {
65

7-
private static int DELAY = 40;
6+
private static int DELAY = 20;
87

98
private HeapSortData data;
109
private AlgoFrame frame;
1110

12-
public AlgoVisualizer(AlgoFrame frame, HeapSortData data){
11+
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
12+
13+
// 初始化数据
14+
data = new HeapSortData(N, sceneHeight);
1315

14-
this.frame = frame;
15-
this.data = data;
16+
// 初始化视图
17+
EventQueue.invokeLater(() -> {
18+
frame = new AlgoFrame("Heap Sort Visualization", sceneWidth, sceneHeight);
1619

17-
this.setData(data.N());
20+
new Thread(() -> {
21+
run();
22+
}).start();
23+
});
1824
}
1925

2026
public void run(){
2127

22-
heapSort();
28+
setData(data.N());
2329

24-
this.setData(0);
25-
AlgoVisHelper.pause(DELAY);
26-
}
27-
28-
private void heapSort(){
29-
30-
for( int i = (data.N()-1-1)/2 ; i >= 0 ; i -- )
30+
for( int i = (data.N()-1-1)/2 ; i >= 0 ; i -- ){
3131
shiftDown(data.N(), i);
32+
}
3233

3334
for( int i = data.N()-1; i > 0 ; i-- ){
3435
data.swap(0, i);
3536
shiftDown(i, 0);
3637
setData(i);
37-
AlgoVisHelper.pause(DELAY);
3838
}
3939

4040
setData(0);
41-
AlgoVisHelper.pause(DELAY);
4241
}
4342

4443
private void shiftDown(int n, int k){
@@ -52,36 +51,26 @@ private void shiftDown(int n, int k){
5251
break;
5352

5453
data.swap(k, j);
55-
setData(-1);
56-
AlgoVisHelper.pause(DELAY);
54+
setData(data.heapIndex);
55+
5756
k = j;
5857
}
5958
}
6059

6160
private void setData(int heapIndex){
62-
if(heapIndex != -1)
63-
data.heapIndex = heapIndex;
64-
frame.setData(data);
61+
62+
data.heapIndex = heapIndex;
63+
64+
frame.render(data);
65+
AlgoVisHelper.pause(DELAY);
6566
}
6667

6768
public static void main(String[] args) {
6869

6970
int sceneWidth = 800;
7071
int sceneHeight = 800;
72+
int N = 100;
7173

72-
EventQueue.invokeLater(() -> {
73-
AlgoFrame frame = new AlgoFrame("Heap Sort Visualization", sceneWidth,sceneHeight);
74-
75-
int N = 200;
76-
// int N = 100;
77-
78-
HeapSortData data = new HeapSortData(N, sceneHeight, false);
79-
// HeapSortData data = new HeapSortData(N, sceneHeight, true);
80-
// HeapSortData data = new HeapSortData(N, sceneHeight/2 - 5, sceneHeight/2 + 5);
81-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
82-
new Thread(() -> {
83-
vis.run();
84-
}).start();
85-
});
74+
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
8675
}
8776
}

‎04-Sort-Visualization/11-Heap-Sort-Visualization/src/HeapSortData.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,21 @@
11
import java.util.Arrays;
2-
import java.util.Random;
3-
42

53
public class HeapSortData {
64

7-
private int N;
8-
95
public int[] numbers;
10-
public int heapIndex;
6+
public int heapIndex; // numbers[heapIndex...N) 已经排好序
117

12-
// 生成N个[0,randomBound)之间的随机数;nearlyOrdered控制随机数是否近乎有序
13-
public HeapSortData(int N, int randomBound, boolean nearlyOrdered){
14-
this.N = N;
8+
public HeapSortData(int N, int randomBound){
159

1610
numbers = new int[N];
1711
heapIndex = N;
1812

1913
for( int i = 0 ; i < N ; i ++)
2014
numbers[i] = (int)(Math.random()*randomBound) + 1;
21-
22-
if(nearlyOrdered){
23-
Arrays.sort(numbers);
24-
int swapTime = (int)(0.02*N);
25-
for(int i = 0 ; i < swapTime; i ++){
26-
int a = (int)(Math.random()*N);
27-
int b = (int)(Math.random()*N);
28-
swap(a, b);
29-
}
30-
}
31-
}
32-
33-
// 生成N个[lBound,rBound]之间的随机数
34-
public HeapSortData(int N, int lBound, int rBound){
35-
this.N = N;
36-
37-
numbers = new int[N];
38-
heapIndex = N;
39-
40-
for( int i = 0 ; i < N ; i ++)
41-
numbers[i] = (int)(Math.random()*(rBound-lBound+1)) + lBound;
4215
}
4316

4417
public int N(){
45-
return N;
18+
return numbers.length;
4619
}
4720

4821
public int get(int index){
@@ -53,6 +26,10 @@ public int get(int index){
5326
}
5427

5528
public void swap(int i, int j) {
29+
30+
if( i < 0 || i >= numbers.length || j < 0 || j >= numbers.length)
31+
throw new IllegalArgumentException("Invalid index to access Sort Data.");
32+
5633
int t = numbers[i];
5734
numbers[i] = numbers[j];
5835
numbers[j] = t;

0 commit comments

Comments
 (0)
Please sign in to comment.