-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
204 lines (168 loc) · 5.87 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bubble Sort (冒泡排序)</title>
<style>
.main {
background-color: antiquewhite;
border-radius: 5px;
}
.bubble circle:hover {
filter: brightness(87%);
}
</style>
</head>
<body>
<h1>Bubble Sort (冒泡排序)
<a href="https://github.com/GaryBikini/BubbleSort">
<img src="https://github.com/favicon.ico" width="18" />
</a>
</h1>
<p>
<input id="sample" type="button" value="Generate" onclick="updateSample();" />
Samples: <span id="sample4show">...</span>
</p>
<p>
Step Time (ms): <span id="tm4show">...</span>
<input id="tm" type="range" min="100" max="3000" step="100" oninput="updateTime(+this.value);" />
</p>
<p>
<input id="btn" type="button" value="Start" onclick="start();" />
<input id="btn" type="button" value="Stop" onclick="stop();" />
</p>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
let arr = [10, 8, 2, 4, 1, 3];
let arrMax = 30;
let arrLen = 6;
let bubbles = [];
let allTimeouts = [];
let stepTime = 1000;
// 1. add svg
let m = { t: 20, b: 20, l: 30, r: 30 };
let w = 405 - m.l - m.r;
let h = 500 - m.t - m.b;
let s = d3.select('body').append('svg')
.attr('class', 'main')
.attr('width', w + m.l + m.r)
.attr('height', h + m.t + m.b)
// 2. add bubbles
addBubbles();
function addBubbles() {
s.selectAll('.bubble').remove();
bubbles = [];
let min = Math.min(...arr);
let max = Math.max(...arr);
let r_min = 12;
let tmp1 = arr.map(a => (a - min) / (max - min));
let tmp2 = tmp1.reduce((a, b) => a + b);
let rs = tmp1.map(a => ((h - (arr.length * r_min * 2)) / 2) * (a / tmp2) + r_min);
let cx = m.l + w / 2;
let cy = m.t;
for (let i = 0; i < arr.length; i++) {
let r = rs[i];
cy += r;
bubbles.push({
id: i,
tagid: "r" + i,
cx: cx,
cy: cy,
r: r,
lbl: arr[i],
ty: 0,
});
cy += r;
}
let g = s.selectAll('.bubble')
.data(bubbles)
.enter().append('g')
.attr('class', 'bubble')
.attr('id', d => d.tagid);
g.append('circle')
.attr('class', 'bubble')
.attr('cx', d => d.cx)
.attr('cy', d => d.cy)
.attr('r', d => d.r)
.attr('fill', d => d3.schemeCategory10[(d.id) % 10]);
g.append('text')
.attr('class', 'text')
.style("fill", "white")
.style('font-size', d => d.r)
.attr("text-anchor", "middle")
.attr("dy", ".35em")
.attr('x', d => d.cx)
.attr('y', d => d.cy)
.text(d => d.lbl)
}
// 3. sort
function sort() {
let schedule = [];
bubbleSort(arr);
function bubbleSort(brr) {
let arr = [...brr];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
schedule.push([j, j + 1]);
let tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
return arr;
}
console.log(schedule);
for (let i = 0; i < schedule.length; i++) {
let delayTime = stepTime * i;
allTimeouts.push(
setTimeout(() => {
let [ia, ib] = schedule[i];
a = bubbles[ia];
b = bubbles[ib];
ea = s.select('#' + a.tagid);
eb = s.select('#' + b.tagid);
a.ty = a.ty + 2 * b.r;
b.ty = b.ty - 2 * a.r;
ea.transition().ease(d3.easeCubicOut).duration(stepTime / 2)
.attr('transform', `translate (0,${a.ty})`)
eb.transition().ease(d3.easeCubicIn).duration(stepTime / 2)
.attr('transform', `translate (0,${b.ty})`)
bubbles[ia] = b;
bubbles[ib] = a;
}, delayTime)
)
}
}
// 4. ui
updateSample();
function updateSample() {
const numbers = Array(arrMax).fill().map((_, i) => i + 1);
numbers.sort(() => Math.random() - 0.5);
arr = numbers.slice(0, arrLen);
arr.sort((a, b) => b - a);
d3.select('#sample4show').text(arr.join(', '));
stop();
}
updateTime(stepTime)
function updateTime(val) {
stepTime = val;
d3.select('#tm').property('value', stepTime);
d3.select('#tm4show').text(stepTime);
stop();
}
function stop() {
allTimeouts.forEach(d => clearTimeout(d));
allTimeouts = [];
addBubbles();
}
function start() {
stop();
sort();
}
</script>
</body>
</html>