-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT192-sort-quick-sort.html
More file actions
292 lines (259 loc) · 7.91 KB
/
T192-sort-quick-sort.html
File metadata and controls
292 lines (259 loc) · 7.91 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>T192 快速排序可视化</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
background: #f5f5f5;
padding: 20px;
text-align: center;
}
#array-container {
display: flex;
justify-content: center;
align-items: flex-end;
height: 300px;
margin-bottom: 5px;
gap: 5px;
}
.bar {
width: 30px;
background-color: steelblue;
color: white;
display: flex;
align-items: flex-end;
justify-content: center;
font-size: 12px;
transition: background-color 0.3s ease;
user-select: none;
border-radius: 3px;
}
.pivot {
background-color: darkorange !important;
}
.left-boundary {
border-bottom: 3px solid limegreen;
}
.right-boundary {
border-bottom: 3px solid crimson;
}
.current-compare {
background-color: #ff6666 !important;
}
.partition-index {
background-color: #3399ff !important;
}
#index-labels, #info-labels {
display: flex;
justify-content: center;
gap: 5px;
margin-bottom: 8px;
user-select: none;
}
#index-labels span, #info-labels span {
width: 30px;
text-align: center;
font-size: 12px;
color: #333;
}
#log {
background-color: black;
color: limegreen;
padding: 10px;
font-family: monospace;
text-align: left;
max-height: 250px;
overflow-y: auto;
white-space: pre-wrap;
margin-bottom: 20px;
border-radius: 4px;
}
input[type=text], input[type=number] {
padding: 5px;
font-size: 14px;
width: 320px;
max-width: 100%;
margin-bottom: 10px;
}
button {
padding: 8px 16px;
font-size: 14px;
margin: 0 5px 15px;
cursor: pointer;
}
footer {
margin-top: 40px;
}
a {
color: #0066cc;
text-decoration: none;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h2>🔍 T192 快速排序算法可视化</h2>
<div>
<label>请输入数组(用逗号分隔):</label><br />
<input id="arrayInput" type="text" value="9,8,7,6,5,4,3,2,1" />
</div>
<div>
<label>动画时间间隔(毫秒):</label>
<input id="delayInput" type="number" value="1000" min="100" max="5000" />
</div>
<div>
<button onclick="startSort()">开始排序</button>
<button onclick="resetArray()">重置数组</button>
</div>
<div id="array-container"></div>
<div id="index-labels"></div>
<div id="info-labels"></div>
<h3>📜 日志输出</h3>
<div id="log"></div>
<footer>
<a href="index.html">🔙 返回首页</a>
</footer>
<script>
const container = document.getElementById("array-container");
const indexLabels = document.getElementById("index-labels");
const infoLabels = document.getElementById("info-labels");
const logEl = document.getElementById("log");
const arrayInput = document.getElementById("arrayInput");
const delayInput = document.getElementById("delayInput");
let nums = [];
let delay = 1000;
let sorting = false;
// 延迟工具函数
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// 渲染数组条形图
function renderArray(arr, highlight = {}) {
container.innerHTML = "";
indexLabels.innerHTML = "";
infoLabels.innerHTML = "";
if (arr.length === 0) return;
const maxVal = Math.max(...arr);
arr.forEach((val, idx) => {
const bar = document.createElement("div");
bar.className = "bar";
bar.style.height = ((val / maxVal) * 100).toFixed(2) + "%";
bar.textContent = val;
if (highlight.pivot === idx) bar.classList.add("pivot");
if (highlight.left === idx) bar.classList.add("left-boundary");
if (highlight.right === idx) bar.classList.add("right-boundary");
if (highlight.current === idx) bar.classList.add("current-compare");
if (highlight.partition === idx) bar.classList.add("partition-index");
container.appendChild(bar);
// 下标
const idxSpan = document.createElement("span");
idxSpan.textContent = idx;
indexLabels.appendChild(idxSpan);
// 辅助信息:显示当前 left, right, pivot,空白则不显示
const infoSpan = document.createElement("span");
let label = "";
if (highlight.left === idx) label = "L";
else if (highlight.right === idx) label = "R";
else if (highlight.pivot === idx) label = "P";
else if (highlight.partition === idx) label = "X";
infoSpan.textContent = label;
infoLabels.appendChild(infoSpan);
});
}
// 日志打印
function log(msg) {
logEl.textContent += msg + "\n";
logEl.scrollTop = logEl.scrollHeight;
}
// 交换数组元素
function swap(arr, i, j) {
if (i !== j) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
// partition 函数,返回分割点索引,配合动画
async function partition(arr, left, right) {
const pivotVal = arr[right];
let px = left;
log(`partition 开始,left=${left}, right=${right}, pivotVal=arr[${right}]=${pivotVal}`);
renderArray(arr, { left, right, pivot: right, partition: px });
await sleep(delay);
for (let i = left; i < right; i++) {
renderArray(arr, { left, right, pivot: right, current: i, partition: px });
log(`比较 arr[${i}]=${arr[i]} 和 pivotVal=${pivotVal}`);
await sleep(delay);
if (arr[i] < pivotVal) {
log(`arr[${i}] < pivotVal,交换 arr[${i}] 和 arr[${px}]`);
swap(arr, px, i);
px++;
renderArray(arr, { left, right, pivot: right, partition: px });
await sleep(delay);
}
}
log(`循环结束,交换 pivot 和 arr[${px}]`);
swap(arr, px, right);
renderArray(arr, { left, right, pivot: px, partition: px });
await sleep(delay);
log(`partition 返回分割点 index=${px}\n`);
return px;
}
// 快速排序递归函数,动画异步实现
async function quickSort(arr, left, right) {
if (left >= right) {
if (left === right) {
log(`单元素区间 left=right=${left}, 无需排序`);
renderArray(arr, { left, right });
await sleep(delay);
}
return;
}
log(`quickSort 递归调用:left=${left}, right=${right}`);
renderArray(arr, { left, right });
await sleep(delay);
const partIx = await partition(arr, left, right);
await quickSort(arr, left, partIx - 1);
await quickSort(arr, partIx + 1, right);
}
// 主入口,启动排序
async function startSort() {
if (sorting) return;
sorting = true;
logEl.textContent = "";
// 解析输入数组
const inputStr = arrayInput.value.trim();
nums = inputStr.split(",").map(s => parseInt(s.trim())).filter(n => !isNaN(n));
if (nums.length === 0) {
alert("请输入有效的数组!");
sorting = false;
return;
}
delay = parseInt(delayInput.value);
if (isNaN(delay) || delay < 100) delay = 1000;
renderArray(nums);
log("开始快速排序...\n");
await quickSort(nums, 0, nums.length - 1);
log("\n排序完成!");
sorting = false;
}
// 重置数组显示为输入框内容
function resetArray() {
if (sorting) return;
const inputStr = arrayInput.value.trim();
nums = inputStr.split(",").map(s => parseInt(s.trim())).filter(n => !isNaN(n));
renderArray(nums);
logEl.textContent = "";
}
// 页面加载时渲染默认数组
window.onload = () => {
resetArray();
};
</script>
</body>
</html>