-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.html
448 lines (360 loc) · 13.2 KB
/
test3.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>排序算法比较3</title>
<style>
.sorting-canvas {
border: 1px solid #000;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div>
<canvas id="bubbleSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>冒泡排序</p>
</div>
<div>
<canvas id="selectionSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>选择排序</p>
</div>
<div>
<canvas id="insertionSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>插入排序</p>
</div>
<div>
<canvas id="mergeSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>归并排序</p>
</div>
<div>
<canvas id="quickSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>快速排序</p>
</div>
<div>
<canvas id="heapSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>堆排序</p>
</div>
<div>
<canvas id="countingSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>计数排序</p>
</div>
<div>
<canvas id="bucketSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>桶排序</p>
</div>
<div>
<canvas id="radixSortCanvas" class="sorting-canvas" width="800" height="100"></canvas>
<p>基数排序</p>
</div>
<script>
const SIZE = 100;
function generateShuffledArray(size) {
const shuffledArray = Array.from({ length: size }, (_, index) => index + 1);
// Fisher-Yates 洗牌算法
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
return shuffledArray;
}
function drawVerticalLines(canvas, dataArray, algorithmName) {
const context = canvas.getContext("2d");
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
// 清空画布
context.clearRect(0, 0, canvasWidth, canvasHeight);
const barWidth = canvasWidth / dataArray.length;
dataArray.forEach((value, index) => {
const barHeight = value * (canvasHeight / SIZE); // 缩放以适应画布高度
const x = index * barWidth;
const y = canvasHeight - barHeight; // 从底部开始画
context.fillStyle = "blue";
context.fillRect(x, y, barWidth, barHeight);
});
// 显示当前排序算法名称(中文)
context.fillStyle = "black";
context.font = "14px Arial";
context.fillText(algorithmName, 10, 20);
}
async function bubbleSort(array, canvas) {
const copiedArray = [...array];
const n = copiedArray.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (copiedArray[j] > copiedArray[j + 1]) {
[copiedArray[j], copiedArray[j + 1]] = [copiedArray[j + 1], copiedArray[j]];
}
drawVerticalLines(canvas, copiedArray, "冒泡排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
}
async function selectionSort(array, canvas) {
const copiedArray = [...array];
const n = copiedArray.length;
for (let i = 0; i < n - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < n; j++) {
if (copiedArray[j] < copiedArray[minIndex]) {
minIndex = j;
}
}
[copiedArray[i], copiedArray[minIndex]] = [copiedArray[minIndex], copiedArray[i]];
drawVerticalLines(canvas, copiedArray, "选择排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
async function insertionSort(array, canvas) {
const copiedArray = [...array];
const n = copiedArray.length;
for (let i = 1; i < n; i++) {
const key = copiedArray[i];
let j = i - 1;
while (j >= 0 && copiedArray[j] > key) {
copiedArray[j + 1] = copiedArray[j];
j--;
}
copiedArray[j + 1] = key;
drawVerticalLines(canvas, copiedArray, "插入排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
function insertionSort2(array, canvas) {
const copiedArray = [...array];
const n = copiedArray.length;
for (let i = 1; i < n; i++) {
const key = copiedArray[i];
let j = i - 1;
while (j >= 0 && copiedArray[j] > key) {
copiedArray[j + 1] = copiedArray[j];
j--;
}
copiedArray[j + 1] = key;
}
return copiedArray;
}
async function runSortingAlgorithm(algorithm, array, canvas) {
await algorithm(array, canvas);
console.log(algorithm.name + " 排序完成");
}
async function mergeSort(array, canvas) {
const copiedArray = [...array];
async function merge(left, middle, right) {
const n1 = middle - left + 1;
const n2 = right - middle;
const leftArray = new Array(n1);
const rightArray = new Array(n2);
for (let i = 0; i < n1; i++) {
leftArray[i] = copiedArray[left + i];
}
for (let j = 0; j < n2; j++) {
rightArray[j] = copiedArray[middle + 1 + j];
}
let i = 0;
let j = 0;
let k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
copiedArray[k] = leftArray[i];
i++;
} else {
copiedArray[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1) {
copiedArray[k] = leftArray[i];
i++;
k++;
}
while (j < n2) {
copiedArray[k] = rightArray[j];
j++;
k++;
}
}
async function mergeSortUtil(left, right) {
if (left < right) {
const middle = Math.floor(left + (right - left) / 2);
await mergeSortUtil(left, middle);
await mergeSortUtil(middle + 1, right);
await merge(left, middle, right);
drawVerticalLines(canvas, copiedArray, "归并排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
await mergeSortUtil(0, copiedArray.length - 1);
}
async function quickSort(array, canvas) {
const copiedArray = [...array];
async function partition(low, high) {
const pivot = copiedArray[high];
let i = low - 1;
for (let j = low; j <= high - 1; j++) {
if (copiedArray[j] < pivot) {
i++;
[copiedArray[i], copiedArray[j]] = [copiedArray[j], copiedArray[i]];
}
}
[copiedArray[i + 1], copiedArray[high]] = [copiedArray[high], copiedArray[i + 1]];
return i + 1;
}
async function quickSortUtil(low, high) {
if (low < high) {
const pi = await partition(low, high);
await quickSortUtil(low, pi - 1);
await quickSortUtil(pi + 1, high);
drawVerticalLines(canvas, copiedArray, "快速排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
await quickSortUtil(0, copiedArray.length - 1);
}
async function heapSort(array, canvas) {
const copiedArray = [...array];
async function heapify(n, i) {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && copiedArray[left] > copiedArray[largest]) {
largest = left;
}
if (right < n && copiedArray[right] > copiedArray[largest]) {
largest = right;
}
if (largest !== i) {
[copiedArray[i], copiedArray[largest]] = [copiedArray[largest], copiedArray[i]];
await heapify(n, largest);
}
}
async function heapSortUtil() {
const n = copiedArray.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
await heapify(n, i);
}
for (let i = n - 1; i >= 0; i--) {
[copiedArray[0], copiedArray[i]] = [copiedArray[i], copiedArray[0]];
await heapify(i, 0);
drawVerticalLines(canvas, copiedArray, "堆排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
await heapSortUtil();
}
async function countingSort(array, canvas) {
const copiedArray = [...array];
const max = Math.max(...copiedArray);
const countArray = new Array(max + 1).fill(0);
for (let i = 0; i < copiedArray.length; i++) {
countArray[copiedArray[i]]++;
}
let k = 0;
for (let i = 0; i <= max; i++) {
while (countArray[i] > 0) {
copiedArray[k] = i;
k++;
countArray[i]--;
drawVerticalLines(canvas, copiedArray, "计数排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
}
async function bucketSort(array, canvas) {
const copiedArray = [...array];
const n = copiedArray.length;
const buckets = new Array(n);
for (let i = 0; i < n; i++) {
buckets[i] = [];
}
console.log("aaa", buckets);
for (let i = 0; i < n; i++) {
const bucketIndex = Math.floor(n * copiedArray[i] / 100)-1;
// console.log(bucketIndex, buckets.length, copiedArray.length, n, copiedArray[i], i);
buckets[bucketIndex].push(copiedArray[i]);
}
console.log("bbb", buckets);
for (let i = 0; i < n; i++) {
buckets[i] = insertionSort2(buckets[i], canvas);
}
console.log("ddd", buckets);
let k = 0;
for (let i = 0; i < n; i++) {
console.log("fff", i, buckets);
for (let j = 0; j < buckets[i].length; j++) {
copiedArray[k] = buckets[i][j];
k++;
drawVerticalLines(canvas, copiedArray, "桶排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
}
async function radixSort(array, canvas) {
const copiedArray = [...array];
const getMax = () => {
let max = copiedArray[0];
for (let i = 1; i < copiedArray.length; i++) {
if (copiedArray[i] > max) {
max = copiedArray[i];
}
}
return max;
};
const countingSortForRadix = async (exp) => {
const n = copiedArray.length;
const output = new Array(n).fill(0);
const count = new Array(10).fill(0);
for (let i = 0; i < n; i++) {
count[Math.floor(copiedArray[i] / exp) % 10]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = n - 1; i >= 0; i--) {
output[count[Math.floor(copiedArray[i] / exp) % 10] - 1] = copiedArray[i];
count[Math.floor(copiedArray[i] / exp) % 10]--;
}
for (let i = 0; i < n; i++) {
copiedArray[i] = output[i];
drawVerticalLines(canvas, copiedArray, "基数排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
};
const max = getMax();
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
await countingSortForRadix(exp);
drawVerticalLines(canvas, copiedArray, "基数排序");
await new Promise(resolve => setTimeout(resolve, 20));
}
}
// 生成一个包含 100 个元素,元素范围在 1 到 100 之间的打乱数组
const dataArray = generateShuffledArray(SIZE);
// 获取 canvas 元素
const bubbleSortCanvas = document.getElementById("bubbleSortCanvas");
const selectionSortCanvas = document.getElementById("selectionSortCanvas");
const insertionSortCanvas = document.getElementById("insertionSortCanvas");
// 绘制初始数组
drawVerticalLines(bubbleSortCanvas, dataArray, "冒泡排序");
drawVerticalLines(selectionSortCanvas, dataArray, "选择排序");
drawVerticalLines(insertionSortCanvas, dataArray, "插入排序");
drawVerticalLines(mergeSortCanvas, dataArray, "归并排序");
drawVerticalLines(quickSortCanvas, dataArray, "快速排序");
drawVerticalLines(heapSortCanvas, dataArray, "堆排序");
drawVerticalLines(countingSortCanvas, dataArray, "计数排序");
drawVerticalLines(bucketSortCanvas, dataArray, "桶排序");
drawVerticalLines(radixSortCanvas, dataArray, "基数排序");
// 执行排序并绘制过程
runSortingAlgorithm(bubbleSort, [...dataArray], bubbleSortCanvas);
runSortingAlgorithm(selectionSort, [...dataArray], selectionSortCanvas);
runSortingAlgorithm(insertionSort, [...dataArray], insertionSortCanvas);
runSortingAlgorithm(mergeSort, [...dataArray], mergeSortCanvas);
runSortingAlgorithm(quickSort, [...dataArray], quickSortCanvas);
runSortingAlgorithm(heapSort, [...dataArray], heapSortCanvas);
runSortingAlgorithm(countingSort, [...dataArray], countingSortCanvas);
runSortingAlgorithm(bucketSort, [...dataArray], bucketSortCanvas);
runSortingAlgorithm(radixSort, [...dataArray], radixSortCanvas);
</script>
</body>
</html>