Skip to content

Commit fea4f4b

Browse files
committed
Chapter 04 section 10 codes updated.
1 parent a40f1df commit fea4f4b

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public AlgoFrame(String title){
3232
public int getCanvasHeight(){return canvasHeight;}
3333

3434
// data
35-
ThreeWaysQuickSortData data;
35+
private ThreeWaysQuickSortData data;
3636
public void render(ThreeWaysQuickSortData data){
3737
this.data = data;
3838
repaint();

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
public class AlgoVisualizer {
55

6-
private static int DELAY = 40;
6+
private static int DELAY = 20;
77

88
private ThreeWaysQuickSortData data;
99
private AlgoFrame frame;
1010

1111
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, ThreeWaysQuickSortData.Type dataType){
1212

1313
// 初始化数据
14-
int randomBound = dataType == ThreeWaysQuickSortData.Type.Identical ? sceneHeight/2 : sceneHeight;
15-
data = new ThreeWaysQuickSortData(N, randomBound, dataType);
14+
data = new ThreeWaysQuickSortData(N, sceneHeight, dataType);
1615

1716
// 初始化视图
1817
EventQueue.invokeLater(() -> {
@@ -24,13 +23,17 @@ public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, ThreeWaysQuickSort
2423
});
2524
}
2625

26+
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
27+
this(sceneWidth, sceneHeight, N, ThreeWaysQuickSortData.Type.Default);
28+
}
29+
2730
public void run(){
2831

2932
setData(-1, -1, -1, -1, -1, -1);
3033

3134
quickSort3Ways(0, data.N()-1);
3235

33-
setData(0, data.N()-1, -1, -1, -1, -1);
36+
setData(-1, -1, -1, -1, -1, -1);
3437
}
3538

3639
private void quickSort3Ways(int l, int r){
@@ -47,9 +50,11 @@ private void quickSort3Ways(int l, int r){
4750

4851
// 随机在arr[l...r]的范围中, 选择一个数值作为标定点pivot
4952
int p = (int)(Math.random()*(r-l+1)) + l;
50-
data.swap(l, p);
53+
setData(l, r, -1, p, -1, -1);
5154

55+
data.swap(l, p);
5256
int v = data.get(l);
57+
setData(l, r, -1, l, -1, -1);
5358

5459
int lt = l; // arr[l+1...lt] < v
5560
int gt = r + 1; // arr[gt...r] > v

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ public ThreeWaysQuickSortData(int N, int randomBound, Type dataType){
2222

2323
int lBound = 1;
2424
int rBound = randomBound;
25-
if(dataType == Type.Identical)
26-
lBound = rBound;
25+
if(dataType == Type.Identical){
26+
int avgNumber = (lBound + rBound) / 2;
27+
lBound = avgNumber;
28+
rBound = avgNumber;
29+
}
2730

2831
for( int i = 0 ; i < N ; i ++) {
2932
numbers[i] = (int)(Math.random()*(rBound-lBound+1)) + lBound;

0 commit comments

Comments
 (0)