-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
184 lines (163 loc) · 5.39 KB
/
app.js
File metadata and controls
184 lines (163 loc) · 5.39 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
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
var msg = document.querySelector("#msg");
var outputSec = document.querySelector(".output-data");
var output = document.querySelector(".output");
var inputSec = document.querySelector("#input");
var startBtn = document.querySelector("#start");
var stopBtn = document.querySelector("#stop");
var stopBtn = document.querySelector("#cmnt");
var allSortOpts = document.querySelectorAll(".opt");
var sortMode = "bubble";
var blockArray = [];
function generateRandomColor() {
let maxVal = 0xffffff; // 16777215
let randomNumber = Math.random() * maxVal;
randomNumber = Math.floor(randomNumber);
randomNumber = randomNumber.toString(16);
let randColor = randomNumber.padStart(6, 0);
return `#${randColor.toUpperCase()}`;
}
class Block {
constructor(value) {
this.crrValue = value;
this.height = `${this.adjustHeight()}px`;
this.width = "max-content";
this.domNode = this.createCrrNode();
this.id = this.domNode.getAttribute("id");
this.applyStyle();
}
createCrrNode() {
let node = document.createElement("div");
node.setAttribute("id", `block-${parseInt(Math.random() * 100000)}`);
node.innerHTML = ` <span class="highlight"> ${this.crrValue} </span>`;
return node;
}
applyStyle() {
this.domNode.style.cssText = `
height: ${this.height};
width: ${this.width};
color: white;
background-color:${generateRandomColor()};
border: 1px solid grey;
border-bottom: none;
padding: 5px 4px;
margin: 2px;
margin-bottom: 0;
font-size: 10px;
display:flex;
justify-content: center;
align-items: flex-start;
`;
}
addInside(container) {
container.insertAdjacentElement("beforeEnd", this.domNode);
}
swap(givenValue) {
console.log("Value Swapped");
console.log("Before swap " + this.crrValue);
console.log("After swap " + givenValue);
}
adjustHeight() {
let max = Block.prototype.maxVal;
let base = 150;
let literalHeight = Math.round((this.crrValue / max) * base);
return literalHeight;
}
}
function setBg(element, color) {
let prev = element.domNode.style.backgroundColor;
element.domNode.style.backgroundColor = color;
return prev;
}
async function resetBg(el, pv) {
await new Promise((done) => setTimeout(() => done(), 1));
return setBg(el, generateRandomColor());
}
async function bubbleSortAlgorithm(unsortedArray) {
let tmp;
for (let i = 0; i < unsortedArray.length; i++) {
for (let j = 0; j < unsortedArray.length - i - 1; j++) {
// let pv1 = blockArray[j].domNode.style.backgroundColor;
// let pv2 = blockArray[j + 1].domNode.style.backgroundColor;
// blockArray[j].domNode.style.backgroundColor = "red";
// blockArray[j + 1].domNode.style.backgroundColor = "red";
setBg(blockArray[j], "red");
setBg(blockArray[j + 1], "red");
if (unsortedArray[j] < unsortedArray[j + 1]) {
tmp = unsortedArray[j + 1];
unsortedArray[j + 1] = unsortedArray[j];
unsortedArray[j] = tmp;
}
resetBg(blockArray[j]);
resetBg(blockArray[j + 1]);
}
}
let sortedArray = unsortedArray;
console.log(sortedArray);
outputSec.innerHTML = "";
console.log(blockArray);
sortedArray.forEach((value, idx) => {
blockArray.forEach((el) => {
if (el.crrValue == value) {
el.addInside(outputSec);
}
});
});
}
function sortArray(mode = "bubble", unsortedArray) {
if (mode == "bubble") {
// console.log(` This is sort mode -> ${unsortedArray}` + sortMode);
bubbleSortAlgorithm(unsortedArray);
} else if (mode == "selection") {
console.log(" This is sort mode -> " + sortMode);
} else if (mode == "quick") {
console.log(" This is sort mode -> " + sortMode);
} else if (mode == "merge") {
console.log(" This is sort mode -> " + sortMode);
}
}
function makeAnimation(unsortedArray) {
outputSec.innerHTML = "";
unsortedArray.forEach((element, i) => {
blockArray[i] = new Block(element);
blockArray[i].addInside(outputSec);
});
startBtn.addEventListener("click", () => {
sortArray("bubble", unsortedArray);
});
stopBtn.addEventListener("click", () => {});
}
function changeInputhandler() {
val = inputSec.value;
data = val.split(" ").filter((v) => Number(v));
data = data.map((el) => Number(el));
// console.log(`Sorting Array ${data}`);
output.style.visibility = "visible";
msg.style.visibility = "visible";
setTimeout(() => (msg.style.visibility = "hidden"), 2000);
if (data.length > 24) {
alert("More than 25 numbers are not allowed");
data = data.filter((el, idx) => {
if (idx <= 24) {
return el;
}
});
reparsed_data = data.reduce((prev, single_num) => prev + " " + single_num);
inputSec.value = reparsed_data;
}
// console.log(Math.max(...data));
Block.prototype.maxVal = Math.max(...data);
makeAnimation(data);
}
function main() {
inputSec.addEventListener("change", changeInputhandler);
// inputSec.addEventListener("input", changeInputhandler);
msg.style.visibility = "hidden";
changeInputhandler();
allSortOpts.forEach((el) => {
el.addEventListener("change", (e) => {
sortMode = e.target.value;
console.log(sortMode);
});
});
}
main();