-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_quick_3way.html
189 lines (183 loc) · 6.2 KB
/
sort_quick_3way.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>三路快速排序</title>
</head>
<body>
<canvas height="410" id="canvas1" style="background:white;" width="800"></canvas>
<script>
// 动画延时
const DELAY = 200;
// 生成数组长度
const ARR_LENGTH = 50;
// 保存排序记录
const STATUS_ARR = [];
const RESULT_DATA = {
// 待排序数组
arr: [],
// 正在对比的区间
compareIndexStart: -1,
compareIndexEnd: -1,
// 标记索引起点
startIndex: -1,
// 标记索引即将放入的位置
signIndex: -1,
// 标记位eq
eqIndex: -1,
// 标记位lt
ltIndex: -1,
// 标记位gt
gtIndex: -1,
// 正在互换的索引
changingIndex: [],
};
function draw(canvas, data) {
let context = canvas.getContext('2d');
let maxHeight = canvas.getAttribute('height');
let width = canvas.getAttribute('width');
let w = width / data.arr.length;
context.clearRect(0, 0, 800, 820);
for (let i = 0; i < data.arr.length; i++) {
if (i === data.startIndex) {
context.fillStyle = 'green';
} else if (data.changingIndex.indexOf(i) !== -1) {
context.fillStyle = 'red';
} else if (i === data.signIndex) {
context.fillStyle = 'lightGreen';
} else if (i === data.eqIndex) {
context.fillStyle = 'lightBlue';
} else if (i === data.ltIndex) {
context.fillStyle = 'LightPink';
} else if (i === data.gtIndex) {
context.fillStyle = 'LightPink';
} else if (i >= data.compareIndexStart && i <= data.compareIndexEnd) {
context.fillStyle = 'lightGray';
} else {
context.fillStyle = 'orange';
}
context.fillRect(i * w, maxHeight - data.arr[i], w - 1, data.arr[i]);
}
if (data.startIndex !== -1) {
context.lineWidth = 1;
context.strokeStyle = 'lightGray';
context.beginPath();
let h = maxHeight - data.arr[data.startIndex];
context.moveTo(data.compareIndexStart * w, h);
context.lineTo((data.compareIndexEnd + 1) * w, h);
context.closePath();
context.stroke();
}
}
function quickSort(arr, start, length) {
if (start >= length - 1) {
return;
}
// 每次取一个随机数与start置换,优化近似排序数组的排序速度
let r = Math.floor(Math.random() * (length - start)) + start;
if (r !== start) {
[arr[r], arr[start]] = [arr[start], arr[r]];
}
let [signLt, signGt] = partition(arr, start, length);
quickSort(arr, start, signLt - 1);
quickSort(arr, signGt + 1, length);
}
function partition(arr, start, length) {
let signLt = start + 1;
let signEq = start + 1;
let signGt = length - 1;
saveStatus({compareIndexStart: start, compareIndexEnd: length - 1, startIndex: start});
while (signEq <= signGt) {
saveStatus({
ltIndex: signLt,
eqIndex: signEq,
gtIndex: signGt
});
if (arr[signEq] === arr[start]) {
signEq++;
} else if (arr[signEq] < arr[start]) {
if (signEq !== signLt) {
saveStatus({changingIndex: [signEq, signLt]});
[arr[signEq], arr[signLt]] = [arr[signLt], arr[signEq]];
saveStatus();
saveStatus({changingIndex: []});
}
signLt++;
signEq++;
} else {
// 大于
if (signEq !== signGt) {
saveStatus({changingIndex: [signEq, signGt]});
[arr[signEq], arr[signGt]] = [arr[signGt], arr[signEq]];
saveStatus();
saveStatus({changingIndex: []});
}
signGt--;
}
}
if (start !== signLt - 1) {
saveStatus({signIndex: signLt - 1});
[arr[start], arr[signLt - 1]] = [arr[signLt - 1], arr[start]];
saveStatus({startIndex: signLt - 1})
}
saveStatus({
compareIndexStart: -1,
compareIndexEnd: -1,
// startIndex: -1,
signIndex: -1,
eqIndex: -1,
ltIndex: -1,
gtIndex: -1,
changingIndex: []
});
return [signLt, signGt];
}
function saveStatus(args = {}) {
if (args.compareIndexStart !== undefined) {
RESULT_DATA.compareIndexStart = args.compareIndexStart;
}
if (args.compareIndexEnd !== undefined) {
RESULT_DATA.compareIndexEnd = args.compareIndexEnd;
}
if (args.startIndex !== undefined) {
RESULT_DATA.startIndex = args.startIndex;
}
if (args.signIndex !== undefined) {
RESULT_DATA.signIndex = args.signIndex;
}
if (args.eqIndex !== undefined) {
RESULT_DATA.eqIndex = args.eqIndex;
}
if (args.ltIndex !== undefined) {
RESULT_DATA.ltIndex = args.ltIndex;
}
if (args.gtIndex !== undefined) {
RESULT_DATA.gtIndex = args.gtIndex;
}
if (args.changingIndex !== undefined) {
RESULT_DATA.changingIndex = args.changingIndex;
}
STATUS_ARR.push(JSON.parse(JSON.stringify(RESULT_DATA)));
}
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() * 5 + 300));
}
quickSort(RESULT_DATA.arr, 0, RESULT_DATA.arr.length);
let i = 0;
setInterval(() => {
if (i < STATUS_ARR.length) {
draw(canvas, STATUS_ARR[i]);
i++;
}
}, DELAY);
}
run('canvas1');
</script>
</body>
</html>