- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
Latest commit
301 lines (280 loc) · 9.02 KB
/
Copy pathindex.html
File metadata and controls
301 lines (280 loc) · 9.02 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
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>冒泡排序动画 - 每趟完成提示</title>
<style>
body { font-family: monospace; text-align: center; margin-top:20px; }
.bars { display: flex; justify-content: center; align-items: flex-end; gap:6px; height:250px; margin:20px; }
.bar { background-color:#3498db; width:50px; transition: all 0.2s; border-radius:4px4px00; text-align: center; color: white; font-weight: bold; position: relative; }
.barspan { position: absolute; top:-24px; width:100%; left:0; color: black; }
.comparing { background-color: orange; }
.swapping { background-color: red; }
.sorted { background-color: green; }
button,select { margin:5px; padding:6px12px; font-size:14px; }
.info { background:#f0f0f0; padding:8px; margin:10px; border-radius:8px; font-weight: bold; }
.controls {
margin:20px0;
display: flex;
gap:12px;
justify-content: center;
flex-wrap: wrap;
}
button {
background:#334155;
border: none;
color: white;
font-size:1rem;
font-weight:600;
padding:10px20px;
border-radius:40px;
cursor: pointer;
transition:0.1s;
font-family: inherit;
}
button:hover {
background:#1e293b;
transform:scale(0.97);
}
.info {
background:#f1f5f9;
padding:0.8rem;
border-radius:2rem;
margin-top:1rem;
font-size:0.9rem;
color:#0f172a;
}
select,input {
padding:8px12px;
border-radius:40px;
border:1px solid #cbd5e1;
font-family: monospace;
background: white;
}
.speed-label {
background:#e2e8f0;
border-radius:40px;
display: inline-flex;
align-items: center;
gap:8px;
padding:01rem;
}
footer {
margin-top:1rem;
font-size:0.75rem;
color:#64748b;
}
</style>
</head>
<body>
<formaction="home.html" method="get">
<buttonid="home">快速排序</button>
</form>
<h2>冒泡排序可视化 (每趟完成提示)</h2>
<divclass="bars" id="bars"></div>
<divclass="controls">
<buttonid="start">▶ 开始排序</button>
<buttonid="reset">🔄 重置数组</button>
<divclass="speed-label">
⚡ 速度
<inputtype="range" id="speedSlider" min="100" max="5000" value="1500" step="10">
<spanid="speedValue">1500</span> ms
</div>
<span>元素个数: <selectid="size">
<optionvalue="5">5</option>
<optionvalue="6">6</option>
<optionvalue="7" selected>7</option>
<optionvalue="8">8</option>
</select></span>
</div>
<divclass="info" id="info">✅ 已重置。点击“开始排序”。</div>
<script>
(function(){
letarr=[];
letbars=[];
letsorting=false;
lettimer=null;
letspeed=1500;
constbarsDiv=document.getElementById('bars');
conststartBtn=document.getElementById('start');
constresetBtn=document.getElementById('reset');
constspeedSlider=document.getElementById('speedSlider');
constspeedValue=document.getElementById('speedValue');
constsizeSelect=document.getElementById('size');
constinfoDiv=document.getElementById('info');
// 动画速度 (毫秒)
letspeedMs=150;
// ---------- 辅助函数 ----------
functionupdateSpeedDisplay(){
speedMs=parseInt(speedSlider.value);
speedValue.innerText=speedMs;
}
// 生成随机数组 (30~160)
functionrandomArray(n){
leta=[];
for(leti=0;i<n;i++)a.push(Math.floor(Math.random()*130)+30);
returna;
}
// 渲染条形图
functionrender(){
barsDiv.innerHTML='';
bars=[];
letmaxVal=Math.max(...arr,1);
for(leti=0;i<arr.length;i++){
letv=arr[i];
letbar=document.createElement('div');
bar.className='bar';
leth=(v/maxVal)*180;
bar.style.height=Math.max(30,h)+'px';
bar.style.width=(600/arr.length)+'px';
letspan=document.createElement('span');
span.innerText=v;
bar.appendChild(span);
barsDiv.appendChild(bar);
bars.push(bar);
}
}
// 清除所有高亮样式
functionclearStyles(){
for(letbofbars)b.classList.remove('comparing','swapping','sorted');
}
// 标记已排序 (绿色)
functionmarkSorted(idx){
if(bars[idx])bars[idx].classList.add('sorted');
}
// 高亮比较中的两个元素
functionmarkCompare(i,j){
if(bars[i])bars[i].classList.add('comparing');
if(bars[j])bars[j].classList.add('comparing');
}
functionunmarkCompare(i,j){
if(bars[i])bars[i].classList.remove('comparing');
if(bars[j])bars[j].classList.remove('comparing');
}
// 交换数据并更新UI
functionswapUI(i,j){
[arr[i],arr[j]]=[arr[j],arr[i]];
letmaxVal=Math.max(...arr,1);
[i,j].forEach(idx=>{
letbar=bars[idx];
letv=arr[idx];
letspan=bar.querySelector('span');
if(span)span.innerText=v;
leth=(v/maxVal)*180;
bar.style.height=Math.max(30,h)+'px';
});
}
// 延迟函数
functiondelay(ms){
returnnewPromise(resolve=>{timer=setTimeout(resolve,ms);});
}
// **** 核心:冒泡排序生成器 (增加每趟完成提示) ****
asyncfunction*bubbleSteps(){
letn=arr.length;
letpass=0;// 当前趟数计数器
for(leti=0;i<n-1;i++){
pass++;
letswapped=false;
// 显示当前趟数开始 (可选)
infoDiv.innerText=`🔁 第 ${pass} 趟排序进行中...`;
for(letj=0;j<n-1-i;j++){
markCompare(j,j+1);
yield'compare';
if(arr[j]>arr[j+1]){
if(bars[j])bars[j].classList.add('swapping');
if(bars[j+1])bars[j+1].classList.add('swapping');
yield'swap_start';
swapUI(j,j+1);
yield'swap_end';
if(bars[j])bars[j].classList.remove('swapping');
if(bars[j+1])bars[j+1].classList.remove('swapping');
swapped=true;
}
unmarkCompare(j,j+1);
yield'compare_end';
}
// 当前趟结束:标记最后一个元素已归位
markSorted(n-1-i);
yield'sorted_mark';
// ✅ 每趟完成提示(关键修改)
infoDiv.innerText=`✅ 第 ${pass} 趟排序完成!最大值已归位到末尾。`;
yield'pass_hint';// 给一点时间让用户看到提示
if(!swapped){
// 如果无交换,剩余所有元素直接标记为已排序
for(letk=0;k<=n-1-i;k++)markSorted(k);
infoDiv.innerText=`🎉 排序提前完成!总共进行了 ${pass} 趟。`;
break;
}
}
if(arr.length>0&&!bars[0]?.classList.contains('sorted'))markSorted(0);
infoDiv.innerText=`🏁 全部排序完成! (共 ${pass} 趟)`;
}
// 运行动画
asyncfunctionrun(){
if(sorting){
if(timer)clearTimeout(timer);
sorting=false;
awaitdelay(20);
}
sorting=true;
clearStyles();
infoDiv.innerText='⚡ 开始冒泡排序...';
letgen=bubbleSteps();
letdone=false;
while(!done&&sorting){
letr=awaitgen.next();
if(r.done){done=true;break;}
letval=r.value;
if(val==='compare')awaitdelay(speed);
elseif(val==='swap_start')awaitdelay(100);
elseif(val==='swap_end')awaitdelay(80);
elseif(val==='compare_end')awaitdelay(speed*0.6);
elseif(val==='sorted_mark')awaitdelay(60);
elseif(val==='pass_hint')awaitdelay(700);// 让提示停留稍久
}
if(sorting){
sorting=false;
}
if(timer)clearTimeout(timer);
timer=null;
}
// 重置所有
functionreset(){
if(sorting){
if(timer)clearTimeout(timer);
sorting=false;
}
letsize=parseInt(sizeSelect.value);
arr=randomArray(size);
render();
clearStyles();
infoDiv.innerText='✅ 已重置新数组。点击“开始排序”。';
}
// 事件绑定
speedSlider.addEventListener('input',()=>{
updateSpeedDisplay();
});
// 按钮事件
startBtn.onclick=()=>{
if(sorting){
sorting=false;
if(timer)clearTimeout(timer);
setTimeout(()=>run(),50);
}else{
run();
}
};
resetBtn.onclick=()=>{
if(sorting){
sorting=false;
if(timer)clearTimeout(timer);
}
reset();
};
speedSlider.oninput=(e)=>speed=parseInt(e.target.value);
sizeSelect.onchange=()=>reset();
reset();// 初始化
})();
</script>
</body>
</html>