-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue_1.html
181 lines (165 loc) · 5.4 KB
/
priority_queue_1.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
<!DOCTYPE html>
<html lang="zh">
<head>
<link rel="stylesheet" href="css/prism.css">
<style>
html,
body {
box-sizing: border-box;
margin: 0;
padding: 0;
height: 100%;
font-size: 12px;
}
body {
min-height: 500px;
}
section {
display: flex;
flex-wrap: wrap;
}
.code {
margin-top: 3px;
}
pre[class*=language-] {
margin: 0;
padding: 0;
}
main {
border-top: 2px solid #ccc;
width: 100%;
height: 66%;
min-height: 400px;
}
</style>
<title>优先级队列(无序数组)</title>
</head>
<body>
<section class="frames"></section>
<div class="code" style="display: none;">
<pre><code class="language-java">public static int binarySearch(int[] a, int target) {
int i = 0, j = a.length - 1;
int candidate = -1;
while (i <= j) {
int m = (i + j) >>> 1;
if (target < a[m]) { // 在左边
j = m - 1;
} else if (a[m] < target) { // 在右边
i = m + 1;
} else {
candidate = m;
j = m - 1;
}
}
return candidate;
}</code></pre>
</div>
<main></main>
<section>
<div style='background-color:#cc99cd; margin: 2px 2px 0 0; padding: 4px 6px;'>索引</div>
<div style='background-color:#67cdcc; margin: 2px 2px 0 0; padding: 4px 6px;'>数据</div>
<div style='background-color:#f08d49; margin: 2px 2px 0 0; padding: 4px 6px;'>比较</div>
</section>
<div style='margin: 2px 2px 0 0; padding: 4px 6px;'>
<div style="margin-bottom: 2px;">
<span>初始队列 </span><input type="text" id='initQueue' class="saveable" value="4,2,1,10,7">
</div>
<div style="margin-bottom: 2px;">
<span>入队 </span><input type="text" id='offered' class="saveable" value="3">
<input style='font-size:12px;' type="button" value="offer()" onclick="offer()">
</div>
<div style="margin-bottom: 2px;">
<span>出队 </span><input style='font-size:12px;' type="button" value="poll()" onclick="poll()">
</div>
<span>动画速度(ms) </span><input type="number" step="100" value="300" id="animate_speed" class="saveable">
<input style='font-size:12px;' type="button" value="保存" onclick="onSave('priority_quque_1')">
</div>
<script src="js/p5.js"></script>
<script src="js/p5-svg.js"></script>
<script src="js/util.js"></script>
<script src="js/prism.js"></script>
<script>
function poll() {
if (dataArray.length == 0) {
return
}
let max = 0
let i = 1
while (i < dataArray.length) {
d.add({ array: dataArray, pointers: [{ index: i, text: 'i' }, { index: max, text: 'M' }] }, frame)
if (dataArray[i] > dataArray[max]) {
max = i
d.add({ array: dataArray, pointers: [{ index: i, text: 'i' }, { index: max, text: 'M' }] }, frame)
}
i++
}
d.add({ array: dataArray, highlights: [max], pointers: [{ index: i, text: 'i' }, { index: max, text: 'M' }] }, frame)
dataArray.splice(max, 1)
d.add({ array: dataArray }, frame)
d.updateFrameButtons()
}
function offer() {
const offered = Number(document.querySelector('#offered').value)
dataArray.push(offered)
d.add({ array: dataArray, highlights: [dataArray.length - 1] }, frame)
d.add({ array: dataArray }, frame)
d.updateFrameButtons()
}
const options = loadOptionsFromStorage('priority_quque_1')
const DIAMETER = 30 // 直径
const RECT_HEIGHT = 30 // 值矩形高度
const SPACING = 1 // 间隙
const INDEX_RECT_HEIGHT = 20 // 索引矩形高度
const POINTER_HEIGHT = 150 // 指针高度, 从底部算
const d = new Draw(options.animate_speed)
const NODE_LEFT_PAD = 250
const LEFT_PAD = 20
const TOP_PAD = 40
let dataArray = options.initQueue.split(',').map(e => Number(e))
function preload() {
// const font = loadFont('JetBrainsMono-Regular.ttf')
}
function setup() {
const WIN_WIDTH = document.querySelector('main').clientWidth
const WIN_HEIGHT = document.querySelector('main').clientHeight
const FONT_SIZE = 10
createCanvas(WIN_WIDTH, WIN_HEIGHT, SVG)
textSize(FONT_SIZE)
textAlign(CENTER)
d.add({ array: dataArray }, frame)
d.updateFrameButtons()
}
function draw() {
d.draw(() => background('#2d2d2d'))
}
/*
array: 数组
pointers: 指针
highlights: 高亮位置
lineNumber: 高亮行号
*/
function frame({ array, pointers, highlights, lineNumber }) {
const pre = document.querySelector('pre')
if (lineNumber > 0) {
pre.setAttribute('data-line', lineNumber)
Prism.highlightAllUnder(pre)
}
let x = LEFT_PAD, y = height - TOP_PAD - DIAMETER / 2
for (let i = 0; i < array.length; i++) {
stroke(255)
pointers.draw2(i, y, x + DIAMETER)
noStroke()
highlights.includes(i) ? fill('#f08d49') : fill('#67cdcc')
rect(x + DIAMETER, y, DIAMETER, DIAMETER)
fill('#ffffff')
text(array[i], x + DIAMETER + DIAMETER / 2, y + DIAMETER / 2 + 4)
fill('#cc99cd')
rect(x, y, DIAMETER, DIAMETER)
fill('#ffffff')
text(i, x + DIAMETER / 2, y + DIAMETER / 2 + 4)
y -= DIAMETER
}
}
</script>
</body>
</html>