-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_visualization.js
134 lines (112 loc) · 4.69 KB
/
sorting_visualization.js
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
const visualContainer = document.querySelector("#visual-container");
function generateBlocks(num = 20) {
if (num && typeof num !== "number") {
alert("First argument must be a typeof Number");
return;
}
for (let i = 0; i < num; i += 1) {
const value = Math.floor(Math.random() * 100);
const block = document.createElement("div");
block.classList.add("block");
block.classList.add("block-unsorted");
block.style.height = `${value * 3}px`;
block.style.width = `calc((70% / ${num}) - 20px)`;
block.style.transform = `translateX(${i * 30}px)`;
const blockLabel = document.createElement("label");
blockLabel.classList.add("block__id");
blockLabel.innerHTML = value;
block.appendChild(blockLabel);
visualContainer.appendChild(block);
}
}
function swap(el1, el2) {
return new Promise(resolve => {
const style1 = window.getComputedStyle(el1);
const style2 = window.getComputedStyle(el2);
const transform1 = style1.getPropertyValue("transform");
const transform2 = style2.getPropertyValue("transform");
el1.style.transform = transform2;
el2.style.transform = transform1;
// Wait for the transition to end!
window.requestAnimationFrame(function () {
setTimeout(() => {
visualContainer.insertBefore(el2, el1);
resolve();
}, 250);
});
});
}
async function bubbleSort(delay = 100) {
if (delay && typeof delay !== "number") {
alert("sort: First argument must be a typeof Number");
return;
}
let blocks = document.querySelectorAll(".block");
for (let i = 0; i < blocks.length - 1; i += 1) {
for (let j = 0; j < blocks.length - i - 1; j += 1) {
blocks[j].classList.remove('block-unsorted');
blocks[j].classList.add('block-comparing');
blocks[j + 1].classList.remove('block-unsorted');
blocks[j + 1].classList.add('block-comparing');
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
const value1 = Number(blocks[j].childNodes[0].innerHTML);
const value2 = Number(blocks[j + 1].childNodes[0].innerHTML);
if (value1 > value2) {
await swap(blocks[j], blocks[j + 1]);
blocks = document.querySelectorAll(".block");
}
blocks[j].classList.remove('block-comparing');
blocks[j].classList.add('block-unsorted');
blocks[j + 1].classList.remove('block-comparing');
blocks[j + 1].classList.add('block-unsorted');
}
blocks[blocks.length - i - 1].classList.remove('block-unsorted');
blocks[blocks.length - i - 1].classList.add('block-sorted');
}
blocks[0].classList.remove('block-unsorted');
blocks[0].classList.add('block-sorted');
}
async function selectionSort(delay = 100) {
if (delay && typeof delay !== "number") {
alert("sort: First argument must be a typeof Number");
return;
}
let blocks = document.querySelectorAll(".block");
for (let i = 0; i < blocks.length - 1; i += 1) {
let minIdx = i;
for (let j = i + 1; j < blocks.length; j += 1) {
blocks[minIdx].classList.remove('block-unsorted');
blocks[minIdx].classList.add('block-comparing');
blocks[j].classList.add('block-comparing');
blocks[j].classList.remove('block-unsorted');
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
const value1 = Number(blocks[minIdx].childNodes[0].innerHTML);
const value2 = Number(blocks[j].childNodes[0].innerHTML);
if (value1 > value2) {
minIdx = j
}
blocks[minIdx].classList.remove('block-comparing');
blocks[minIdx].classList.add('block-unsorted');
blocks[j].classList.remove('block-comparing');
blocks[j].classList.add('block-unsorted');
}
if (minIdx != i) {
await swap(blocks[i], blocks[minIdx]);
blocks = document.querySelectorAll(".block");
}
blocks[i].classList.remove('block-unsorted');
blocks[i].classList.add('block-sorted');
}
blocks[blocks.length - 1].classList.remove('block-unsorted');
blocks[blocks.length - 1].classList.add('block-sorted');
}
generateBlocks();
bubbleSort();