-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT192-sort-bubble-sort.html
More file actions
147 lines (133 loc) · 3.91 KB
/
T192-sort-bubble-sort.html
File metadata and controls
147 lines (133 loc) · 3.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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>T192 冒泡排序可视化</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
background: #f0f0f0;
}
.controls {
margin-bottom: 20px;
}
#visual {
display: flex;
align-items: flex-end;
height: 300px;
margin-bottom: 20px;
gap: 5px;
}
.bar {
width: 30px;
background-color: #4caf50;
text-align: center;
color: white;
transition: height 0.3s ease, background-color 0.3s ease;
}
.bar.active {
background-color: orange !important;
}
.bar.sorted {
background-color: gray !important;
}
#log {
background-color: #111;
color: #0f0;
padding: 10px;
height: 200px;
overflow-y: auto;
font-family: monospace;
white-space: pre-wrap;
}
input[type="text"], input[type="number"] {
padding: 5px;
width: 300px;
margin-bottom: 10px;
}
button {
padding: 8px 16px;
font-size: 16px;
}
a {
display: inline-block;
margin-top: 20px;
}
</style>
</head>
<body>
<h2>🌀 T192 冒泡排序可视化</h2>
<div class="controls">
输入数组(用逗号分隔):<br>
<input type="text" id="inputArray" value="5,1,4,2,8"><br>
动画间隔(毫秒):<input type="number" id="interval" value="1000" min="100" max="5000"><br>
<button onclick="startSort()">可视化排序</button>
</div>
<div id="visual"></div>
<div id="log"></div>
<a href="index.html">← 返回首页</a>
<script>
let bars = [];
function log(msg) {
const logBox = document.getElementById('log');
logBox.innerText += msg + "\n";
logBox.scrollTop = logBox.scrollHeight;
}
function render(array, highlight = [], sorted = []) {
const container = document.getElementById('visual');
container.innerHTML = '';
const maxVal = Math.max(...array);
bars = [];
array.forEach((val, idx) => {
const bar = document.createElement('div');
bar.classList.add('bar');
if (highlight.includes(idx)) bar.classList.add('active');
if (sorted.includes(idx)) bar.classList.add('sorted');
bar.style.height = `${(val / maxVal) * 100 + 50}px`;
bar.innerText = val;
container.appendChild(bar);
bars.push(bar);
});
}
async function bubbleSortVisual(arr, delay) {
const len = arr.length;
for (let i = 0; i < len; i++) {
let swapped = false;
for (let j = 0; j < len - 1 - i; j++) {
render(arr, [j, j + 1]);
log(`比较 nums[${j}]=${arr[j]} 和 nums[${j+1}]=${arr[j+1]}`);
await new Promise(r => setTimeout(r, delay));
if (arr[j] > arr[j + 1]) {
log(`交换 nums[${j}] 和 nums[${j+1}]:${arr[j]} > ${arr[j+1]}`);
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
swapped = true;
render(arr, [j, j + 1]);
await new Promise(r => setTimeout(r, delay));
}
}
log(`第 ${i + 1} 轮结束:${arr.join(', ')}`);
if (!swapped) {
log('没有发生交换,提前结束排序。');
break;
}
}
render(arr, [], Array.from(arr.keys())); // 标记全部为 sorted
log(`✅ 排序完成:${arr.join(', ')}`);
}
function startSort() {
const input = document.getElementById('inputArray').value;
const interval = parseInt(document.getElementById('interval').value) || 1000;
const arr = input.split(',').map(x => parseInt(x.trim())).filter(x => !isNaN(x));
if (arr.length === 0) {
alert("请输入有效的数组");
return;
}
document.getElementById('log').innerText = '';
log(`开始排序:${arr.join(', ')}`);
render(arr);
bubbleSortVisual(arr, interval);
}
</script>
</body>
</html>