-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (120 loc) · 4.27 KB
/
Copy pathscript.js
File metadata and controls
133 lines (120 loc) · 4.27 KB
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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const algoSelect = document.getElementById('algoSelect');
const sizeSlider = document.getElementById('sizeSlider');
const speedSlider = document.getElementById('speedSlider');
const sizeVal = document.getElementById('sizeVal');
const speedVal = document.getElementById('speedVal');
const sortBtn = document.getElementById('sortBtn');
const shuffleBtn = document.getElementById('shuffleBtn');
let array = [], sorting = false;
const W = canvas.width, H = canvas.height;
function genArray() {
const size = parseInt(sizeSlider.value);
array = Array.from({ length: size }, () => Math.floor(Math.random() * (H - 20)) + 10);
draw(-1, -1, []);
}
function draw(compareIdx, swapIdx, sorted) {
ctx.clearRect(0, 0, W, H);
const barW = W / array.length;
for (let i = 0; i < array.length; i++) {
let color = '#6366f1';
if (sorted.includes(i)) color = '#10b981';
else if (i === swapIdx) color = '#f43f5e';
else if (i === compareIdx) color = '#fbbf24';
ctx.fillStyle = color;
ctx.fillRect(i * barW, H - array[i], barW - 1, array[i]);
}
}
function delay() { return 110 - parseInt(speedSlider.value); }
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
async function bubbleSort() {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (!sorting) return;
draw(j, -1, []);
await sleep(delay());
if (array[j] > array[j + 1]) { [array[j], array[j + 1]] = [array[j + 1], array[j]]; draw(j, j + 1, []); await sleep(delay()); }
}
}
}
async function selectionSort() {
for (let i = 0; i < array.length; i++) {
let min = i;
for (let j = i + 1; j < array.length; j++) {
if (!sorting) return;
draw(j, min, []);
await sleep(delay());
if (array[j] < array[min]) min = j;
}
[array[i], array[min]] = [array[min], array[i]];
draw(i, min, []);
await sleep(delay());
}
}
async function insertionSort() {
for (let i = 1; i < array.length; i++) {
let j = i;
while (j > 0 && array[j - 1] > array[j]) {
if (!sorting) return;
draw(j - 1, j, []);
await sleep(delay());
[array[j], array[j - 1]] = [array[j - 1], array[j]];
j--;
}
}
}
async function quickSort(lo = 0, hi = array.length - 1) {
if (lo >= hi || !sorting) return;
let pivot = array[hi], i = lo;
for (let j = lo; j < hi; j++) {
if (!sorting) return;
draw(j, hi, []);
await sleep(delay());
if (array[j] < pivot) { [array[i], array[j]] = [array[j], array[i]]; i++; }
}
[array[i], array[hi]] = [array[hi], array[i]];
await quickSort(lo, i - 1);
await quickSort(i + 1, hi);
}
async function mergeSort(lo = 0, hi = array.length - 1) {
if (lo >= hi || !sorting) return;
const mid = Math.floor((lo + hi) / 2);
await mergeSort(lo, mid);
await mergeSort(mid + 1, hi);
await merge(lo, mid, hi);
}
async function merge(lo, mid, hi) {
const left = array.slice(lo, mid + 1), right = array.slice(mid + 1, hi + 1);
let i = 0, j = 0, k = lo;
while (i < left.length && j < right.length) {
if (!sorting) return;
draw(k, -1, []);
await sleep(delay());
if (left[i] <= right[j]) array[k++] = left[i++];
else array[k++] = right[j++];
}
while (i < left.length) array[k++] = left[i++];
while (j < right.length) array[k++] = right[j++];
}
async function startSort() {
if (sorting) return;
sorting = true;
sortBtn.disabled = true;
const algo = algoSelect.value;
if (algo === 'bubble') await bubbleSort();
else if (algo === 'selection') await selectionSort();
else if (algo === 'insertion') await insertionSort();
else if (algo === 'quick') await quickSort();
else if (algo === 'merge') await mergeSort();
sorting = false;
sortBtn.disabled = false;
// Final animation
for (let i = 0; i < array.length; i++) { draw(-1, -1, Array.from({ length: i }, (_, k) => k)); await sleep(15); }
draw(-1, -1, array.map((_, i) => i));
}
sizeSlider.addEventListener('input', () => { sizeVal.textContent = sizeSlider.value; if (!sorting) genArray(); });
speedSlider.addEventListener('input', () => { speedVal.textContent = speedSlider.value; });
sortBtn.addEventListener('click', startSort);
shuffleBtn.addEventListener('click', () => { if (!sorting) genArray(); });
genArray();