-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT192-sort-merge-sort.html
More file actions
191 lines (171 loc) · 5.09 KB
/
T192-sort-merge-sort.html
File metadata and controls
191 lines (171 loc) · 5.09 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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>T192 归并排序可视化</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
.bar-container {
position: relative;
display: flex;
align-items: flex-end;
height: 200px;
margin-bottom: 10px;
border: 1px solid #ccc;
padding: 10px;
gap: 5px;
}
.bar {
width: 30px;
background-color: steelblue;
text-align: center;
color: white;
}
.bar.highlight {
background-color: orange;
}
.pointer-row {
display: flex;
justify-content: flex-start;
height: 20px;
margin-bottom: 20px;
font-size: 14px;
line-height: 1;
padding-left: 10px;
gap: 5px;
}
.pointer-label {
width: 30px;
text-align: center;
}
.log {
background: #111;
color: #0f0;
padding: 10px;
height: 200px;
overflow-y: auto;
white-space: pre-line;
font-family: monospace;
}
.controls {
margin-bottom: 10px;
}
input {
padding: 5px;
width: 300px;
}
button {
padding: 5px 10px;
margin-left: 5px;
}
a {
display: inline-block;
margin-top: 20px;
}
</style>
</head>
<body>
<h2>T192 归并排序可视化</h2>
<div class="controls">
输入数组(逗号分隔):
<input type="text" id="arrayInput" value="5,2,9,1,5,6" />
动画间隔(ms):
<input type="number" id="intervalInput" value="1000" min="100" />
<button onclick="startVisualization()">可视化</button>
</div>
<div class="bar-container" id="barContainer"></div>
<div class="pointer-row" id="pointerRow"></div>
<div class="log" id="logBox"></div>
<a href="index.html">← 返回首页</a>
<script>
let delay = 1000;
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
function log(message) {
const logBox = document.getElementById("logBox");
logBox.textContent += message + "\n";
logBox.scrollTop = logBox.scrollHeight;
}
function renderBars(arr, highlightRange = [], pointerPositions = {}) {
const container = document.getElementById("barContainer");
const pointerRow = document.getElementById("pointerRow");
container.innerHTML = "";
pointerRow.innerHTML = "";
const maxVal = Math.max(...arr);
arr.forEach((val, idx) => {
const bar = document.createElement("div");
bar.className = "bar";
bar.style.height = `${(val / maxVal) * 100}%`;
bar.textContent = val;
if (highlightRange.includes(idx)) {
bar.classList.add("highlight");
}
container.appendChild(bar);
const label = document.createElement("div");
label.className = "pointer-label";
let symbols = "";
if (pointerPositions.L === idx) symbols += "L";
if (pointerPositions.M === idx) symbols += "M";
if (pointerPositions.R === idx) symbols += "R";
label.textContent = symbols;
pointerRow.appendChild(label);
});
}
async function mergeSortVisual(arr, left, right) {
if (left >= right) return;
const mid = Math.floor((left + right) / 2);
renderBars(arr, [], { L: left, M: mid, R: right });
log(`拆分:left=${left}, mid=${mid}, right=${right}`);
await sleep(delay);
await mergeSortVisual(arr, left, mid);
await mergeSortVisual(arr, mid + 1, right);
await mergeVisual(arr, left, mid, right);
}
async function mergeVisual(arr, left, mid, right) {
const temp = [];
let i = left, j = mid + 1;
log(`合并:left=${left}, mid=${mid}, right=${right}`);
while (i <= mid && j <= right) {
renderBars(arr, [i, j], { L: left, M: mid, R: right });
await sleep(delay);
if (arr[i] <= arr[j]) {
temp.push(arr[i++]);
} else {
temp.push(arr[j++]);
}
}
while (i <= mid) {
renderBars(arr, [i], { L: left, M: mid, R: right });
await sleep(delay);
temp.push(arr[i++]);
}
while (j <= right) {
renderBars(arr, [j], { L: left, M: mid, R: right });
await sleep(delay);
temp.push(arr[j++]);
}
for (let k = 0; k < temp.length; k++) {
arr[left + k] = temp[k];
renderBars(arr, [left + k], { L: left, M: mid, R: right });
await sleep(delay);
}
log(`合并完成:${arr.slice(left, right + 1).join(", ")}`);
}
async function startVisualization() {
let input = document.getElementById("arrayInput").value.trim();
let arr = input.split(",").map((x) => parseInt(x.trim())).filter(x => !isNaN(x));
delay = parseInt(document.getElementById("intervalInput").value) || 1000;
if (!arr.length) {
alert("请输入有效的整数数组");
return;
}
document.getElementById("logBox").textContent = "";
renderBars(arr);
await mergeSortVisual(arr, 0, arr.length - 1);
log("排序完成!最终结果:" + arr.join(", "));
}
</script>
</body>
</html>