-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
123 lines (112 loc) · 3.34 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="wrapper">
</div>
<div>
<button onclick="handleChose('shellSort')">希尔排序</button>
<button onclick="handleChose('insertSort')">插入排序</button>
<button onclick="handleChose('bubbleSort')">冒泡排序</button>
<button onclick="handleChose('choiceSort')">选择排序</button>
<button onclick="handleChose('quickSort')">快速排序</button>
<button onclick="handleChose('binarySearch', false)">二分法查找</button>
<button onclick="handleShuffle()">重新打乱数组</button>
<button onClick="handleNext()">单步调试(下一步)</button>
<button onClick="handleAll()">一次执行完动画</button>
<button onClick="setSpeed(0.5)">0.5倍速度</button>
<button onClick="setSpeed(1)">1倍速度</button>
<button onClick="setSpeed(2)">2倍速度</button>
<button onClick="setSpeed(4)">4倍速度</button>
<button onClick="setSpeed(8)">8倍速度</button>
<button onClick="setSpeed(16)">16倍速度</button>
<h2 id="info"></h2>
</div>
</body>
<style>
html, body {
width: 100%;
}
.wrapper {
position: relative;
top: 200px;
}
.child {
position: absolute;
background: #fff;
border-radius: 50%;
border:1px solid red;
text-align: center;
}
#info {
color: red;
}
.flag-desc {
position: absolute;
top: -32px;
font-weight: bold;
color: red;
width: 200px;
text-align: center;
left: -78px;
}
</style>
<script src="render.js"></script>
<script src="alg.js"></script>
<script>
let arr = generateArr(15);
const baseSpeed = render.speed;
shuffle(arr);
function generateArr (length) {
const arr = Array.apply(null, { length }).map((i, index) => i = index);
return arr;
}
function handleNext (event) {
render.next();
}
function handleAll () {
render.renderAnimation();
}
function handleReset () {
render.reset();
}
function handleShuffle () {
shuffle(arr);
}
function setSpeed (ratio) {
const speed = baseSpeed / ratio;
render.setSpeed(speed);
}
// 打乱数组的顺序
function shuffle (arr) {
const getRandom = (start, end) => start + Math.floor(Math.random() * (end - start));
const exchange = (arr, x, y) => [arr[x], arr[y]] = [arr[y], arr[x]];
const len = arr.length;
for (let i = 0; i < len - 1; i++) {
const y = getRandom(i, len);
exchange(arr, i, y);
}
}
function handleChose (t, isSort = true) {
render.reset();
let opts = [];
if (isSort) {
render.render(arr);
opts = Sort.run(arr.slice(), t);
} else {
let rawArr = generateArr(15);
render.render(rawArr);
opts = Sort.run(rawArr, t);
}
render.reset();
render.pushAction(opts);
render.speed = 50;
console.log('arr', arr);
}
</script>
</html>