-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT192-sort-insert-sort.html
More file actions
146 lines (132 loc) · 3.85 KB
/
T192-sort-insert-sort.html
File metadata and controls
146 lines (132 loc) · 3.85 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
<!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.insert {
background-color: red !important;
}
.bar.sorted {
background-color: gray !important;
}
#log {
background-color: #111;
color: #0f0;
padding: 10px;
height: 220px;
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,2,4,6,1,3"><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>
function log(msg) {
const logBox = document.getElementById('log');
logBox.innerText += msg + "\n";
logBox.scrollTop = logBox.scrollHeight;
}
function render(array, highlight = [], insertPos = -1, sortedEnd = -1) {
const container = document.getElementById('visual');
container.innerHTML = '';
const maxVal = Math.max(...array);
array.forEach((val, idx) => {
const bar = document.createElement('div');
bar.classList.add('bar');
if (highlight.includes(idx)) bar.classList.add('active');
if (idx === insertPos) bar.classList.add('insert');
if (idx <= sortedEnd) bar.classList.add('sorted');
bar.style.height = `${(val / maxVal) * 100 + 50}px`;
bar.innerText = val;
container.appendChild(bar);
});
}
async function insertionSortVisual(arr, delay) {
let n = arr.length;
render(arr, [], -1, 0);
await new Promise(r => setTimeout(r, delay));
for (let i = 1; i < n; i++) {
let curNum = arr[i];
let j = i - 1;
log(`\n第 ${i} 步,处理 nums[${i}] = ${curNum}`);
while (j >= 0 && arr[j] > curNum) {
log(` nums[${j}] = ${arr[j]} > ${curNum},向右移动`);
arr[j + 1] = arr[j];
render(arr, [j, j + 1], -1, i - 1);
await new Promise(r => setTimeout(r, delay));
j--;
}
log(` 插入位置:${j + 1}`);
arr[j + 1] = curNum;
render(arr, [], j + 1, i);
await new Promise(r => setTimeout(r, delay));
}
log(`\n✅ 排序完成:${arr.join(', ')}`);
render(arr, [], -1, n - 1);
}
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);
insertionSortVisual(arr, interval);
}
</script>
</body>
</html>