-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT164-bucket-sort.html
More file actions
325 lines (291 loc) · 8.64 KB
/
T164-bucket-sort.html
File metadata and controls
325 lines (291 loc) · 8.64 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>T164 最大间距桶排序可视化</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
margin: 20px;
background: #f5f5f5;
}
h1 {
text-align: center;
}
a#homeLink {
display: inline-block;
margin-bottom: 15px;
text-decoration: none;
font-weight: bold;
color: #007acc;
}
.input-area {
margin-bottom: 15px;
text-align: center;
}
input[type="text"] {
width: 60%;
padding: 6px 10px;
font-size: 16px;
}
input[type="number"] {
width: 60px;
padding: 4px;
font-size: 16px;
}
button {
margin-left: 10px;
padding: 6px 12px;
font-size: 16px;
cursor: pointer;
}
#arrayContainer {
margin: 20px auto;
width: 90%;
display: flex;
justify-content: center;
gap: 6px;
flex-wrap: wrap;
}
.array-item {
width: 40px;
height: 40px;
background: #3498db;
color: white;
display: flex;
justify-content: center;
align-items: center;
border-radius: 6px;
font-weight: bold;
user-select: none;
transition: background-color 0.3s;
}
.highlight {
background-color: #e74c3c !important;
}
.bucket {
border: 2px dashed #888;
border-radius: 6px;
padding: 10px;
margin: 10px;
min-width: 60px;
min-height: 40px;
text-align: center;
background-color: #ecf0f1;
position: relative;
}
.bucket-label {
font-size: 12px;
margin-bottom: 4px;
color: #555;
}
#bucketsContainer {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin-top: 20px;
gap: 6px;
}
#log {
background-color: #111;
color: #eee;
font-family: monospace;
padding: 12px;
height: 200px;
overflow-y: auto;
white-space: pre-wrap;
border-radius: 6px;
margin-top: 20px;
}
#infoArea {
margin-top: 15px;
font-size: 16px;
text-align: center;
min-height: 24px;
}
</style>
</head>
<body>
<h1>力扣 T164 最大间距桶排序可视化</h1>
<a href="index.html" id="homeLink">【返回首页】</a>
<div class="input-area">
<label for="inputArray">输入数组(逗号分隔): </label>
<input type="text" id="inputArray" value="3,6,9,1,24,25,48" />
<label for="intervalInput">动画间隔(秒):</label>
<input type="number" id="intervalInput" value="1" min="0.1" step="0.1" />
<button id="startBtn">开始可视化</button>
</div>
<div id="arrayContainer"></div>
<div id="bucketsContainer"></div>
<div id="infoArea"></div>
<div id="log"></div>
<script>
(() => {
const startBtn = document.getElementById("startBtn");
const inputArray = document.getElementById("inputArray");
const intervalInput = document.getElementById("intervalInput");
const arrayContainer = document.getElementById("arrayContainer");
const bucketsContainer = document.getElementById("bucketsContainer");
const logArea = document.getElementById("log");
const infoArea = document.getElementById("infoArea");
let animationInterval = 1000;
let timerId = null;
// 渲染数组
function renderArray(arr, highlightIndices = []) {
arrayContainer.innerHTML = "";
arr.forEach((v, i) => {
const div = document.createElement("div");
div.className = "array-item";
div.textContent = v;
if (highlightIndices.includes(i)) {
div.classList.add("highlight");
}
arrayContainer.appendChild(div);
});
}
// 渲染桶状态
function renderBuckets(buckets, min, bucketWidth, highlightIndex = -1) {
bucketsContainer.innerHTML = "";
buckets.forEach((bucket, i) => {
const div = document.createElement("div");
div.className = "bucket";
if (i === highlightIndex) {
div.style.backgroundColor = "#f39c12";
} else {
div.style.backgroundColor = "#ecf0f1";
}
const label = document.createElement("div");
label.className = "bucket-label";
label.textContent = `桶${i}`;
div.appendChild(label);
if (bucket[0] === Number.MAX_SAFE_INTEGER) {
div.appendChild(document.createTextNode("空"));
} else {
div.appendChild(document.createTextNode(`最小: ${bucket[0]}, 最大: ${bucket[1]}`));
}
bucketsContainer.appendChild(div);
});
}
// 输出日志,自动滚动到底部
function log(msg) {
logArea.textContent += msg + "\n";
logArea.scrollTop = logArea.scrollHeight;
}
// 清理日志和信息
function resetUI() {
logArea.textContent = "";
infoArea.textContent = "";
bucketsContainer.innerHTML = "";
arrayContainer.innerHTML = "";
}
// 主动画函数:桶排序可视化
async function visualizeMaximumGap(arr, intervalSec) {
resetUI();
if (arr.length < 2) {
infoArea.textContent = "数组长度小于2,无需排序,最大间距为0";
log("数组长度小于2,直接返回0");
renderArray(arr);
return;
}
log(`输入数组: [${arr.join(", ")}]`);
renderArray(arr);
let min = Number.MAX_SAFE_INTEGER;
let max = Number.MIN_SAFE_INTEGER;
for (const num of arr) {
if (num > max) max = num;
if (num < min) min = num;
}
log(`最小值: ${min}, 最大值: ${max}`);
if (min === max) {
log("最小值和最大值相同,最大间距为0");
infoArea.textContent = "所有元素相同,最大间距为0";
return;
}
const bucketSize = arr.length + 1;
let buckets = new Array(bucketSize);
for (let i = 0; i < bucketSize; i++) {
// 用大整数代替Integer.MAX_VALUE
buckets[i] = [Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER];
}
// 计算桶宽度
const bucketWidth = Math.ceil((max - min) / bucketSize);
log(`桶数量: ${bucketSize}, 桶宽度: ${bucketWidth}`);
// 显示初始空桶
renderBuckets(buckets, min, bucketWidth);
// 放入桶中
for (let i = 0; i < arr.length; i++) {
const num = arr[i];
let bucketIndex = Math.floor((num - min) / bucketWidth);
if (bucketIndex >= bucketSize) bucketIndex = bucketSize - 1;
infoArea.textContent = `第${i + 1}个元素 ${num} 放入桶 ${bucketIndex}`;
log(`元素 ${num} 计算桶索引: ${bucketIndex}`);
// 高亮当前元素
renderArray(arr, [i]);
// 高亮当前桶
renderBuckets(buckets, min, bucketWidth, bucketIndex);
// 更新桶内最大最小值
buckets[bucketIndex][0] = Math.min(buckets[bucketIndex][0], num);
buckets[bucketIndex][1] = Math.max(buckets[bucketIndex][1], num);
log(`更新桶${bucketIndex} => 最小值: ${buckets[bucketIndex][0]}, 最大值: ${buckets[bucketIndex][1]}`);
await sleep(intervalSec);
}
// 计算最大间距
let maxGap = 0;
let prev = min;
infoArea.textContent = "开始计算最大间距...";
renderBuckets(buckets, min, bucketWidth);
for (let i = 0; i < buckets.length; i++) {
if (buckets[i][0] === Number.MAX_SAFE_INTEGER) {
log(`桶${i}为空,跳过`);
continue;
}
let gap = buckets[i][0] - prev;
log(`桶${i}的最小值 ${buckets[i][0]} - 上一个最大值 ${prev} = 间距 ${gap}`);
if (gap > maxGap) {
maxGap = gap;
log(`更新最大间距为 ${maxGap}`);
}
prev = buckets[i][1];
infoArea.textContent = `检查桶${i},当前最大间距: ${maxGap}`;
renderBuckets(buckets, min, bucketWidth, i);
await sleep(intervalSec);
}
infoArea.textContent = `最大间距计算完成: ${maxGap}`;
log(`返回最大间距: ${maxGap}`);
// 最后高亮全部数组
renderArray(arr);
renderBuckets(buckets, min, bucketWidth);
}
function sleep(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
// 事件绑定
startBtn.addEventListener("click", () => {
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
const rawInput = inputArray.value.trim();
if (!rawInput) {
alert("请输入有效数组!");
return;
}
let arr = rawInput.split(",").map(s => parseInt(s.trim())).filter(n => !isNaN(n));
if (arr.length === 0) {
alert("请输入有效数字数组!");
return;
}
let intervalSec = parseFloat(intervalInput.value);
if (isNaN(intervalSec) || intervalSec <= 0) {
intervalSec = 1;
}
animationInterval = intervalSec;
visualizeMaximumGap(arr, animationInterval);
});
// 页面加载时默认渲染数组
renderArray(inputArray.value.split(",").map(s => parseInt(s.trim())).filter(n => !isNaN(n)));
})();
</script>
</body>
</html>