-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_bubble2.html
173 lines (164 loc) · 5.49 KB
/
sort_bubble2.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
<!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: 40%;
min-height: 200px;
}
</style>
<title>冒泡排序_改进1</title>
</head>
<body>
<section class="frames"></section>
<div class="code">
<pre><code class="language-java">public static void bubble(int[] a) {
final int n = a.length - 1;
for (int j = 0; j < n; j++) {
boolean swapped = false;
for (int i = 0; i < n - j; i++) {
if (a[i] > a[i + 1]) {
swap(a, i, i + 1);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}</code></pre>
</div>
<main></main>
<section>
<div style='background-color:#67cdcc; margin: 2px 2px 0 0; padding: 4px 6px;'>未排序区域</div>
<div style='background-color:#ccc; margin: 2px 2px 0 0; padding: 4px 6px;'>已排序区域</div>
<div style='background-color:#cc99cd; 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;'>
<span>初始数组 </span><input type="text" id='data' class="saveable" value="3,2,4,1,6,5,7">
<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('sort_bubble2')">
</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>
const options = loadOptionsFromStorage('sort_bubble2')
const RECT_WIDTH = 30 // 矩形宽度、圆直径
const SPACING = 1 // 间隙
const INDEX_RECT_HEIGHT = 20 // 索引矩形高度
const POINTER_HEIGHT = 150 // 指针高度, 从底部算
const VALUE_RECT_MIN_HEIGHT = 20 // 值矩形最小高度
const VALUE_RECT_MAX_HEIGHT = 100 // 值矩形最大高度
const d = new Draw(options.animate_speed)
let data = options.data.split(',').map(e => Number(e))
let heightMap = new Map()
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)
heightMap = calculateRectHeight(data, VALUE_RECT_MIN_HEIGHT, VALUE_RECT_MAX_HEIGHT)
bubble(data)
d.updateFrameButtons()
}
function draw() {
d.draw(() => background('#2d2d2d'))
}
/*
array: 数组
highlights: 高亮位置
pointers: 指针
unsorted: 未排序个数
lineNumber: 高亮行号
*/
function frame({ array, pointers, highlights, lineNumber, unsorted }) {
const pre = document.querySelector('pre')
if (lineNumber > 0) {
pre.setAttribute('data-line', lineNumber)
Prism.highlightAllUnder(pre)
}
const LEFT = (width - array.length * (RECT_WIDTH + SPACING)) / 2
for (let i = 0; i < array.length; i++) {
// 注:矩形以左下角 x, y 作为起点坐标
let x = LEFT + i * (RECT_WIDTH + SPACING)
let y = height - (SPACING + INDEX_RECT_HEIGHT)
stroke(255)
pointers.draw(i, x + RECT_WIDTH / 2, INDEX_RECT_HEIGHT + SPACING * 2)
if (i > unsorted) {
fill('#ccc')
} else {
highlights.includes(i) ? fill('#f08d49') : fill('#67cdcc')
}
noStroke()
rect(x, y, RECT_WIDTH, -heightMap.get(array[i]))
fill('#ffffff')
text(array[i], x + RECT_WIDTH / 2, y - 6)
fill('#cc99cd')
rect(x, height, RECT_WIDTH, -INDEX_RECT_HEIGHT)
fill('#ffffff')
text(i, x + RECT_WIDTH / 2, height - 6)
}
}
function bubble(a) {
const n = a.length - 1
d.add({ array: a, unsorted: n }, frame)
for (let j = 0; j < a.length - 1; j++) {
let swapped = false
for (let i = 0; i < n - j; i++) {
d.add({ array: a, pointers: [{ index: i, text: 'i' }, { index: i + 1, text: 'i+1' }], unsorted: n - j, lineNumber: 6 }, frame)
if (a[i] > a[i + 1]) {
d.add({ array: a, pointers: [{ index: i, text: 'i' }, { index: i + 1, text: 'i+1' }], highlights: [i, i + 1], unsorted: n - j, lineNumber: 7 }, frame)
swap(a, i, i + 1)
d.add({ array: a, pointers: [{ index: i, text: 'i' }, { index: i + 1, text: 'i+1' }], highlights: [i, i + 1], unsorted: n - j, lineNumber: 8 }, frame)
swapped = true
}
}
if (!swapped) {
d.add({ array: a, unsorted: n - j, lineNumber: 11 }, frame)
d.add({ array: a, unsorted: -1, lineNumber: 12 }, frame)
break
}
}
d.add({ array: a, unsorted: -1 }, frame)
}
function swap(a, i, j) {
let k = a[i]
a[i] = a[j]
a[j] = k
}
</script>
</body>
</html>