-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_quick_2way.html
147 lines (142 loc) · 5.09 KB
/
sort_quick_2way.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双路快速排序</title>
</head>
<body>
<canvas height="410" id="canvas1" style="background:white;" width="800"></canvas>
<script>
// 动画延时
const DELAY = 200;
// 生成数组长度
const ARR_LENGTH = 50;
// 保存排序记录
const STATUS_ARR = [];
const RESULT_DATA = {
// 待排序数组
arr: [],
// 正在对比的区间
compareIndexStart: -1,
compareIndexEnd: -1,
// 标记索引起点
startIndex: -1,
// 标记索引即将放入的位置
signIndex: -1,
// 正在对比的索引
comparingIndex: [],
// 正在互换的索引
changingIndex: [],
};
function draw(canvas, data) {
let context = canvas.getContext('2d');
let maxHeight = canvas.getAttribute('height');
let width = canvas.getAttribute('width');
let w = width / data.arr.length;
context.clearRect(0, 0, 800, 820);
for (let i = 0; i < data.arr.length; i++) {
if (data.changingIndex.indexOf(i) !== -1) {
context.fillStyle = 'red';
} else if (data.comparingIndex.indexOf(i) !== -1) {
context.fillStyle = 'lightBlue';
} else if (i === data.startIndex) {
context.fillStyle = 'green';
} else if (i === data.signIndex) {
context.fillStyle = 'lightGreen';
} else if (i >= data.compareIndexStart && i <= data.compareIndexEnd) {
context.fillStyle = 'lightGray';
} else {
context.fillStyle = 'orange';
}
context.fillRect(i * w, maxHeight - data.arr[i], w - 1, data.arr[i]);
}
if (data.startIndex !== -1) {
context.lineWidth = 1;
context.strokeStyle = 'lightGray';
context.beginPath();
let h = maxHeight - data.arr[data.startIndex];
context.moveTo(data.compareIndexStart * w, h);
context.lineTo((data.compareIndexEnd + 1) * w, h);
context.closePath();
context.stroke();
}
}
function quickSort(arr, start, length) {
if (start >= length - 1) {
return;
}
// 每次取一个随机数与start置换,优化近似排序数组的排序速度
let r = Math.floor(Math.random() * (length - start)) + start;
if (r !== start) {
[arr[r], arr[start]] = [arr[start], arr[r]];
}
let sign = partition(arr, start, length);
quickSort(arr, start, sign);
quickSort(arr, sign + 1, length);
}
function partition(arr, start, length) {
let signLeft = start + 1;
let signRight = length - 1;
RESULT_DATA.startIndex = start;
RESULT_DATA.compareIndexStart = start;
RESULT_DATA.compareIndexEnd = length - 1;
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
while (signLeft <= signRight) {
RESULT_DATA.comparingIndex = [signLeft, signRight];
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
if (arr[signLeft] < arr[start]) {
signLeft++;
continue;
}
if (arr[signRight] > arr[start]) {
signRight--;
continue;
}
// 此时signLeft值>=start值且signRight值<=start
if (signLeft !== signRight) {
RESULT_DATA.changingIndex = [signLeft, signRight];
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
[arr[signLeft], arr[signRight]] = [arr[signRight], arr[signLeft]];
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
RESULT_DATA.changingIndex = [];
}
signLeft++;
signRight--;
}
RESULT_DATA.comparingIndex = [];
if (start !== signRight) {
RESULT_DATA.signIndex = signRight;
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
[arr[start], arr[signRight]] = [arr[signRight], arr[start]];
RESULT_DATA.startIndex = signRight;
RESULT_DATA.signIndex = -1;
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
}
RESULT_DATA.compareIndexStart = -1;
RESULT_DATA.compareIndexEnd = -1;
RESULT_DATA.startIndex = -1;
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
return signRight;
}
function run(id) {
let canvas = document.getElementById(id);
if (!canvas) {
return false;
}
// 生成随机数组
for (let i = 0; i < ARR_LENGTH; i++) {
RESULT_DATA.arr.push(Math.floor(Math.random() * 400 + 1));
}
quickSort(RESULT_DATA.arr, 0, RESULT_DATA.arr.length);
let i = 0;
setInterval(() => {
if (i < STATUS_ARR.length) {
draw(canvas, STATUS_ARR[i]);
i++;
}
}, DELAY);
}
run('canvas1');
</script>
</body>
</html>