-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_selection.html
105 lines (101 loc) · 3.75 KB
/
sort_selection.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选择排序</title>
</head>
<body>
<canvas height="600" id="canvas1" style="background:white;" width="500"></canvas>
<script>
// 动画延时
const DELAY = 200;
// 生成数组长度
const ARR_LENGTH = 50;
const RESULT_DATA = {
// 待排序数组
arr: [],
// 当前正在对比的索引
compareIndex: 0,
// 正在对比的最小值
minIndex: 0,
// 已对比的索引结束位置
sortedIndex: 0,
// 标记排序中
isSorting: false,
};
function draw(canvas, data) {
// 获取上下文对象
let context = canvas.getContext('2d');
let maxHeight = canvas.getAttribute('height');
let width = canvas.getAttribute('width');
// 清空画布
context.clearRect(0, 0, 1500, 600);
for (let i = 0; i < data.arr.length; i++) {
if (i <= data.sortedIndex) {
context.fillStyle = 'orange';
} else if (i === data.minIndex) {
context.fillStyle = data.isSorting ? 'Teal' : 'violet';
} else if (i === data.compareIndex) {
context.fillStyle = 'LightBlue';
} else {
context.fillStyle = data.isSorting && i === data.sortedIndex + 1 ? 'Teal' : 'lightGray';
}
let w = width / data.arr.length;
context.fillRect(i * w, maxHeight - data.arr[i], w-1, data.arr[i]);
}
}
function selectionSort() {
let statusArr = [];
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
for (let i = 0; i < RESULT_DATA.arr.length; i++) {
RESULT_DATA.minIndex = i;
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
for (let j = i + 1; j < RESULT_DATA.arr.length; j++) {
// 标记当前正在对比的索引
RESULT_DATA.compareIndex = j;
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
if (RESULT_DATA.arr[j] < RESULT_DATA.arr[RESULT_DATA.minIndex]) {
RESULT_DATA.minIndex = j;
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
}
}
if (RESULT_DATA.minIndex !== i) {
// 交换
RESULT_DATA.isSorting = true;
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
[RESULT_DATA.arr[i], RESULT_DATA.arr[RESULT_DATA.minIndex]] = [RESULT_DATA.arr[RESULT_DATA.minIndex], RESULT_DATA.arr[i]];
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
RESULT_DATA.isSorting = false;
// statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
}
// 记录已排序索引
RESULT_DATA.sortedIndex = i;
statusArr.push(JSON.parse(JSON.stringify(RESULT_DATA)));
}
return statusArr;
}
function run(id) {
let canvas = document.getElementById(id);
if (!canvas) {
return false;
}
// 生成随机数组
for (let i = 0; i < ARR_LENGTH; i++) {
RESULT_DATA.arr.push(Math.floor(Math.random() * 580 + 1));
}
// 排序时,保存每次变动时的属性,返回变动属性的数组
let statusArr = selectionSort();
let i = 0;
setInterval(() => {
if (i < statusArr.length) {
draw(canvas, statusArr[i]);
i++;
}
}, DELAY);
}
run('canvas1');
</script>
</body>
</html>