Repository navigation
Expand file tree
/
Copy pathalgovisualizer.js
More file actions
145 lines (111 loc) · 4.7 KB
/
Copy pathalgovisualizer.js
File metadata and controls
145 lines (111 loc) · 4.7 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
134
135
136
137
138
139
140
141
142
143
144
145
const container = document.querySelector(".data-container");
document.getElementById("Button3").disabled = true;
document.getElementById("Button3").style.backgroundColor = "rgb(89, 93, 97)";
// function to generate bars
function generatebars(num = 20) {
//for loop to generate 20 bars
for (let i = 0; i < num; i += 1) {
// To generate random values from 1 to 100
const value = Math.floor(Math.random() * 100) + 1;
// To create element "div"
const bar = document.createElement("div");
// To add class "bar" to "div"
bar.classList.add("bar");
// Provide height to the bar
bar.style.height = `${value * 3}px`;
// Translate the bar towards positive X axis
bar.style.transform = `translateX(${i * 30}px)`;
// To create element "label"
const barLabel = document.createElement("label");
// To add class "bar_id" to "label"
barLabel.classList.add("bar_id");
// Assign value to "label"
barLabel.innerHTML = value;
// Append "Label" to "div"
bar.appendChild(barLabel);
// Append "div" to "data-container div"
container.appendChild(bar);
}
}
// asynchronous function to perform "Selection Sort"
async function SelectionSort(delay = 50) {
let bars = document.querySelectorAll(".bar");
// Assign 0 to min_idx
var min_idx = 0;
for (var i = 0; i < bars.length; i += 1) {
// Assign i to min_idx
min_idx = i;
// Provide darkblue color to the ith bar
bars[i].style.backgroundColor = "darkblue";
for (var j = i + 1; j < bars.length; j += 1) {
// Provide red color to the jth bar
bars[j].style.backgroundColor = "red";
// To pause the execution of code for 300 milliseconds
await new Promise((resolve) =>
setTimeout(() => {
resolve();
}, 50)
);
// To store the integer value of jth bar to var1
var val1 = parseInt(bars[j].childNodes[0].innerHTML);
// To store the integer value of (min_idx)th bar to var2
var val2 = parseInt(bars[min_idx].childNodes[0].innerHTML);
// Compare val1 & val2
if (val1 < val2) {
if (min_idx !== i) {
// Provide skyblue color to the (min-idx)th bar
bars[min_idx].style.backgroundColor = " rgb(145, 197, 197)";
}
min_idx = j;
} else {
// Provide skyblue color to the jth bar
bars[j].style.backgroundColor = " rgb(145, 197, 197)";
}
}
// To swap ith and (min_idx)th bar
var temp1 = bars[min_idx].style.height;
var temp2 = bars[min_idx].childNodes[0].innerText;
bars[min_idx].style.height = bars[i].style.height;
bars[i].style.height = temp1;
bars[min_idx].childNodes[0].innerText = bars[i].childNodes[0].innerText;
bars[i].childNodes[0].innerText = temp2;
// To pause the execution of code for 300 milliseconds
await new Promise((resolve) =>
setTimeout(() => {
resolve();
}, 200)
);
// Provide skyblue color to the (min-idx)th bar
bars[min_idx].style.backgroundColor = " rgb(145, 197, 197)";
// Provide lightgreen color to the ith bar
bars[i].style.backgroundColor = " rgb(49, 226, 13)";
}
// To enable the button "Generate New Array" after final(sorted)
document.getElementById("Button1").disabled = false;
document.getElementById("Button1").style.backgroundColor = "rgb(112, 128, 144)";
// To enable the button "Selection Sort" after final(sorted)
document.getElementById("Button2").disabled = false;
document.getElementById("Button2").style.backgroundColor = "rgb(112, 128, 144)";
}
// Call "generatebars" function
generatebars();
// function to generate new random array
function generate() {
window.location.reload();
}
// function to toggle the buttons
function togglebtns() {
// To disable the button "Generate New Array"
document.getElementById("Button1").disabled = true;
document.getElementById("Button1").style.backgroundColor = "rgb(89, 93, 97)";
// To disable the button "Selection Sort"
document.getElementById("Button2").disabled = true;
document.getElementById("Button2").style.backgroundColor = "rgb(89, 93, 97)";
// To enable the button "Stop"
document.getElementById("Button3").disabled = false;
document.getElementById("Button3").style.backgroundColor = "rgb(112, 128, 144)";
}
// function to stop the sort
function stop() {
window.location.reload();
}