Skip to content

Commit 23e454e

Browse files
committed
Chapter 04 section 10 codes updated.
1 parent ce80b19 commit 23e454e

File tree

5 files changed

+58
-68
lines changed

5 files changed

+58
-68
lines changed

04-Sort-Visualization/10-Three-Ways-Quick-Sort-Visualization/src/AlgoFrame.java

Lines changed: 6 additions & 13 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

@@ -39,7 +33,7 @@ public AlgoFrame(String title){
3933

4034
// data
4135
ThreeWaysQuickSortData data;
42-
public void setData(ThreeWaysQuickSortData data){
36+
public void render(ThreeWaysQuickSortData data){
4337
this.data = data;
4438
repaint();
4539
}
@@ -66,18 +60,17 @@ 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 ++ ) {
71-
if ( data.l != -1 && data.r != -1 && i >= data.l && i <= data.r)
64+
if ( i >= data.l && i <= data.r)
7265
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Green);
7366
else
7467
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Grey);
7568

76-
if( data.curPivot != -1 && i == data.curPivot )
69+
if( i == data.curPivot )
7770
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Indigo);
78-
if( data.l != -1 && data.curL != -1 && i >= data.l + 1 && i <= data.curL)
71+
if( i >= data.l + 1 && i <= data.curL)
7972
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
80-
if( data.r != -1 && data.curR != -1 && i >= data.curR && i <= data.r)
73+
if( i >= data.curR && i <= data.r)
8174
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
8275
if( data.fixedPivots[i] )
8376
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);

04-Sort-Visualization/10-Three-Ways-Quick-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: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import java.awt.*;
2-
import java.util.Random;
3-
import java.util.Arrays;
2+
43

54
public class AlgoVisualizer {
65

@@ -9,20 +8,29 @@ public class AlgoVisualizer {
98
private ThreeWaysQuickSortData data;
109
private AlgoFrame frame;
1110

12-
public AlgoVisualizer(AlgoFrame frame, ThreeWaysQuickSortData data){
11+
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, ThreeWaysQuickSortData.Type dataType){
12+
13+
// 初始化数据
14+
int randomBound = dataType == ThreeWaysQuickSortData.Type.Identical ? sceneHeight/2 : sceneHeight;
15+
data = new ThreeWaysQuickSortData(N, randomBound, dataType);
1316

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

17-
this.setData(-1, -1, -1, -1, -1, -1);
21+
new Thread(() -> {
22+
run();
23+
}).start();
24+
});
1825
}
1926

2027
public void run(){
2128

29+
setData(-1, -1, -1, -1, -1, -1);
30+
2231
quickSort3Ways(0, data.N()-1);
2332

24-
this.setData(0, data.N()-1, -1, -1, -1, -1);
25-
AlgoVisHelper.pause(DELAY);
33+
setData(0, data.N()-1, -1, -1, -1, -1);
2634
}
2735

2836
private void quickSort3Ways(int l, int r){
@@ -32,12 +40,10 @@ private void quickSort3Ways(int l, int r){
3240

3341
if( l == r ) {
3442
setData(l, r, l, -1, -1, -1);
35-
AlgoVisHelper.pause(DELAY);
3643
return;
3744
}
3845

3946
setData(l, r, -1, -1, -1, -1);
40-
AlgoVisHelper.pause(DELAY);
4147

4248
// 随机在arr[l...r]的范围中, 选择一个数值作为标定点pivot
4349
int p = (int)(Math.random()*(r-l+1)) + l;
@@ -49,7 +55,6 @@ private void quickSort3Ways(int l, int r){
4955
int gt = r + 1; // arr[gt...r] > v
5056
int i = l+1; // arr[lt+1...i) == v
5157
setData(l, r, -1, l, lt, gt);
52-
AlgoVisHelper.pause(DELAY);
5358

5459
while( i < gt ){
5560
if( data.get(i) < v ){
@@ -64,13 +69,11 @@ else if( data.get(i) > v ){
6469
else // arr[i] == v
6570
i ++;
6671

67-
setData(l, r, -1, l, lt, gt);
68-
AlgoVisHelper.pause(DELAY);
72+
setData(l, r, -1, l, i, gt);
6973
}
7074

7175
data.swap( l, lt );
7276
setData(l, r, lt, -1, -1, -1);
73-
AlgoVisHelper.pause(DELAY);
7477

7578
quickSort3Ways(l, lt-1 );
7679
quickSort3Ways(gt, r);
@@ -90,27 +93,20 @@ private void setData(int l, int r, int fixedPivot, int curPivot, int curL, int c
9093
data.curPivot = curPivot;
9194
data.curL = curL;
9295
data.curR = curR;
93-
frame.setData(data);
96+
97+
frame.render(data);
98+
AlgoVisHelper.pause(DELAY);
9499
}
95100

96101
public static void main(String[] args) {
97102

98103
int sceneWidth = 800;
99104
int sceneHeight = 800;
105+
int N = 100;
100106

101-
EventQueue.invokeLater(() -> {
102-
AlgoFrame frame = new AlgoFrame("Three Ways Quick Sort Visualization", sceneWidth,sceneHeight);
107+
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N, ThreeWaysQuickSortData.Type.Default);
108+
// AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N, ThreeWaysQuickSortData.Type.NearlyOrdered);
109+
// AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N, ThreeWaysQuickSortData.Type.Identical);
103110

104-
int N = 200;
105-
// int N = 100;
106-
107-
// ThreeWaysQuickSortData data = new ThreeWaysQuickSortData(N, sceneHeight, false);
108-
// ThreeWaysQuickSortData data = new ThreeWaysQuickSortData(N, sceneHeight, true);
109-
ThreeWaysQuickSortData data = new ThreeWaysQuickSortData(N, sceneHeight/2 - 5, sceneHeight/2 + 5);
110-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
111-
new Thread(() -> {
112-
vis.run();
113-
}).start();
114-
});
115111
}
116112
}

04-Sort-Visualization/10-Three-Ways-Quick-Sort-Visualization/src/ThreeWaysQuickSortData.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
import java.util.Arrays;
2-
import java.util.Random;
32

43

54
public class ThreeWaysQuickSortData {
65

7-
private int N;
6+
public enum Type{
7+
Default,
8+
NearlyOrdered,
9+
Identical
10+
}
811

9-
public int[] numbers;
12+
private int[] numbers;
1013
public int l, r;
1114
public boolean[] fixedPivots;
1215
public int curPivot;
1316
public int curL, curR;
1417

15-
// 生成N个[0,randomBound)之间的随机数;nearlyOrdered控制随机数是否近乎有序
16-
public ThreeWaysQuickSortData(int N, int randomBound, boolean nearlyOrdered){
17-
this.N = N;
18+
public ThreeWaysQuickSortData(int N, int randomBound, Type dataType){
1819

1920
numbers = new int[N];
2021
fixedPivots = new boolean[N];
2122

23+
int lBound = 1;
24+
int rBound = randomBound;
25+
if(dataType == Type.Identical)
26+
lBound = rBound;
27+
2228
for( int i = 0 ; i < N ; i ++) {
23-
numbers[i] = (int)(Math.random()*randomBound) + 1;
29+
numbers[i] = (int)(Math.random()*(rBound-lBound+1)) + lBound;
2430
fixedPivots[i] = false;
2531
}
2632

27-
if(nearlyOrdered){
33+
if(dataType == Type.NearlyOrdered){
2834
Arrays.sort(numbers);
29-
int swapTime = (int)(0.02*N);
35+
int swapTime = (int)(0.01*N);
3036
for(int i = 0 ; i < swapTime; i ++){
3137
int a = (int)(Math.random()*N);
3238
int b = (int)(Math.random()*N);
@@ -35,19 +41,12 @@ public ThreeWaysQuickSortData(int N, int randomBound, boolean nearlyOrdered){
3541
}
3642
}
3743

38-
// 生成N个[lBound,rBound]之间的随机数
39-
public ThreeWaysQuickSortData(int N, int lBound, int rBound){
40-
this.N = N;
41-
42-
numbers = new int[N];
43-
fixedPivots = new boolean[N];
44-
45-
for( int i = 0 ; i < N ; i ++)
46-
numbers[i] = (int)(Math.random()*(rBound-lBound+1)) + lBound;
44+
public ThreeWaysQuickSortData(int N, int randomBound){
45+
this(N, randomBound, Type.Default);
4746
}
4847

4948
public int N(){
50-
return N;
49+
return numbers.length;
5150
}
5251

5352
public int get(int index){
@@ -58,6 +57,10 @@ public int get(int index){
5857
}
5958

6059
public void swap(int i, int j) {
60+
61+
if( i < 0 || i >= numbers.length || j < 0 || j >= numbers.length)
62+
throw new IllegalArgumentException("Invalid index to access Sort Data.");
63+
6164
int t = numbers[i];
6265
numbers[i] = numbers[j];
6366
numbers[j] = t;

04-Sort-Visualization/Chapter-04.key

151 KB
Binary file not shown.

0 commit comments

Comments
 (0)