-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathT035-binary-search-insert.html
More file actions
202 lines (170 loc) · 4.68 KB
/
T035-binary-search-insert.html
File metadata and controls
202 lines (170 loc) · 4.68 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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>🔍 搜索插入位置可视化</title>
<style>
body {
font-family: "Helvetica Neue", sans-serif;
background-color: #f2f4f8;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
color: #333;
}
.container {
max-width: 800px;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
label {
display: block;
margin: 10px 0 5px;
font-weight: bold;
}
input[type="text"], input[type="number"] {
width: 100%;
padding: 10px;
font-size: 16px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
font-size: 16px;
margin-top: 10px;
border: none;
background-color: #007bff;
color: white;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.array-box {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin-top: 20px;
}
.array-item {
width: 40px;
height: 40px;
margin: 5px;
text-align: center;
line-height: 40px;
border-radius: 5px;
background-color: #e0e0e0;
font-weight: bold;
}
.left {
background-color: #ffcc80 !important;
}
.mid {
background-color: #90caf9 !important;
}
.right {
background-color: #a5d6a7 !important;
}
.result {
margin-top: 20px;
font-size: 18px;
font-weight: bold;
text-align: center;
}
.nav {
text-align: center;
margin-top: 30px;
}
.nav a {
text-decoration: none;
color: #007bff;
font-size: 16px;
}
.nav a:hover {
text-decoration: underline;
}
.step {
font-family: monospace;
background: #f7f7f7;
padding: 10px;
margin-top: 10px;
border-radius: 5px;
overflow-x: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>🔍 搜索插入位置(二分查找可视化)</h1>
<label for="arrayInput">输入有序数组(用逗号分隔):</label>
<input type="text" id="arrayInput" value="1,3,5,6" />
<label for="targetInput">目标值:</label>
<input type="number" id="targetInput" value="5" />
<button onclick="startVisualization()">开始查找</button>
<div class="array-box" id="arrayDisplay"></div>
<div class="result" id="resultText"></div>
<div class="step" id="log"></div>
<div class="nav">
<a href="index.html">← 返回首页</a>
</div>
</div>
<script>
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function startVisualization() {
const input = document.getElementById("arrayInput").value;
const target = parseInt(document.getElementById("targetInput").value);
const arr = input.split(",").map(s => parseInt(s.trim())).filter(n => !isNaN(n));
const log = document.getElementById("log");
log.innerText = "";
if (arr.length === 0 || isNaN(target)) {
alert("请正确输入数组和目标值!");
return;
}
const arrayDisplay = document.getElementById("arrayDisplay");
const resultText = document.getElementById("resultText");
arrayDisplay.innerHTML = "";
resultText.innerText = "";
let left = 0, right = arr.length - 1;
while (left <= right) {
renderArray(arr, left, right, Math.floor((left + right) / 2));
const mid = Math.floor((left + right) / 2);
log.innerText += `left=${left}, mid=${mid}, right=${right}, value=${arr[mid]}\n`;
await sleep(1000);
if (arr[mid] === target) {
resultText.innerText = `🎯 找到目标值,索引为 ${mid}`;
return;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
renderArray(arr, left, -1, -1);
resultText.innerText = `➕ 未找到目标值,应插入索引为 ${left}`;
}
function renderArray(arr, left, right, mid) {
const display = document.getElementById("arrayDisplay");
display.innerHTML = "";
arr.forEach((val, idx) => {
const div = document.createElement("div");
div.className = "array-item";
if (idx === mid) div.classList.add("mid");
if (idx === left) div.classList.add("left");
if (idx === right) div.classList.add("right");
div.innerText = val;
display.appendChild(div);
});
}
</script>
</body>
</html>