-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
296 lines (277 loc) · 11 KB
/
main.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
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
// THIS IS THE MAIN JS CODE.
//====================================== MAIN PROJECT CORE ALGROTHIMS ===============================================================
//Variable block generation
var mySound = new sound("bounce.mp3");
var numberOfBlocks = parseInt($("#Range").val());
var delayTime = parseInt($("#myTime").val());
const container = document.querySelector(".data-container");
var sortingOperation = false;
var exitLoop = false;
//on click on the input checkbox input show the algorithm title on body.
$("input").click(function() {
var text = $("input:checked").val();
$(".header").text(text);
});
//on click on Random Block Generate , call generateBlocks
$("#randblkGenBtn").click(function() {
if (sortingOperation) {
return alert("SORTING operation is RUNNING PLEASE Wait");
}
generateBlocks();
});
//on click sort start sorting.
$("#sort").click(function() {
if ($("input:checked").val() === undefined) {
return alert("Please select sorting algorithm");
}
if (sortingOperation) {
return alert("SORTING OPERATION IS RUNNING PLEASE WAIT");
}
mergeSort();
bubbleSort();
selectionSort();
});
//on click on slider show the number of blocks
$("#Range").click(function() {
if (sortingOperation) {
return alert("Please wait until the sorting is done");
}
numberOfBlocks = parseInt($("#Range").val());
generateBlocks();
$("#valOfSlider").text(numberOfBlocks + " Blocks");
});
//on click on slider show animation delay.
$("#myTime").click(function() {
if (sortingOperation) {
return alert("Please wait until the sorting is done");
}
delayTime = parseInt($("#myTime").val());
$("#timeOfSlider").text(delayTime + " ms ");
});
//on stop click stop the sorting
$("#stop").click(function() {
if (sortingOperation === false) {
return alert("The sorting has already stopped");
}
exitLoop = true;
console.log(exitLoop);
});
//This function is for sound effect
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function() {
this.sound.play();
};
this.stop = function() {
this.sound.pause();
};
}
//============================>RANDOM BLOCK GENERATOR<==========================//
function generateBlocks(num = numberOfBlocks) {
$(container).html("");
if (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);
//create cnost of div
const block = document.createElement("div");
//add class named block
block.classList.add("block");
//set the height of the block to a random generated value scaled by 3.
block.style.height = `${value * 3}px`;
//set a block width of 30 px.
block.style.transform = `translateX(${i * 30}px)`;
const blockLabel = document.createElement("label");
blockLabel.classList.add("block__id");
blockLabel.innerHTML = value;
block.appendChild(blockLabel);
$(container).append(block);
}
}
//** each page refresh will generate a new random block
generateBlocks();
//**
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");
//fliping the styling of the element
el1.style.transform = transform2;
el2.style.transform = transform1;
// Wait for the transition to end!
window.requestAnimationFrame(function() {
setTimeout(() => {
container.insertBefore(el2, el1);
resolve();
}, 250);
});
});
}
//===============================>Bubble Sorted Funvtion<===========================//
//bubble function that takes the delay time value in ms;
async function bubbleSort(delay = delayTime) {
if ($("input:checked").val() === "Bubble Sort") {
sortingOperation = true;
if (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++) {
for (let j = 0; j < blocks.length - i - 1; j++) {
if (exitLoop === true) {
break;
} else {
blocks[j].style.backgroundColor = "#FF4949";
blocks[j + 1].style.backgroundColor = "#FF4949";
//making delay time , asyncrounsly form the main excution time.
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
//takes the values of index and the following index from blocks div
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]); //calling the swap function and waiting until the transition ends
blocks = document.querySelectorAll(".block");
}
mySound.play();
//set element current index and following to different color style.
blocks[j].style.backgroundColor = "#58B7FF";
blocks[j + 1].style.backgroundColor = "#58B7FF";
//set the last element color to green.
blocks[blocks.length - i - 1].style.backgroundColor = "#13CE66";
//setting the operation flage to false, ending of sorting .
}
sortingOperation = false;
exitLoop = false;
}
}
}
}
//==============================>MERGE SORTER FUNCTION<=============================//
async function mergeSort(delay = delayTime) {
if ($("input:checked").val() === "Merge Sort") {
sortingOperation = true;
if (typeof delay !== "number") {
alert("sort: First argument must be a typeof Number");
return;
}
let blocks = document.querySelectorAll(".block");
const blocksMiddleIdx = Math.floor((blocks.length - 1) / 2);
//THIS PART FOR THE FIRST HALF OF THE BLOCK.
for (let i = 0; i < blocksMiddleIdx; i++) {
for (let j = 0; j < blocksMiddleIdx - i; j++) {
//this will break the function if the stop button cliked.
if (exitLoop === true) {
break;
} else {
blocks[j].style.backgroundColor = "#FF4949";
blocks[j + 1].style.backgroundColor = "#FF4949";
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
//takes the values of index and the followig index from blocks div
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]); //calling the swap function and waiting until the transition ends
blocks = document.querySelectorAll(".block");
}
//play the sound effect.
mySound.play();
//set element current index and following to different color style.
blocks[j].style.backgroundColor = "#58B7FF";
blocks[j + 1].style.backgroundColor = "#58B7FF";
//set the last element color to green.
blocks[blocksMiddleIdx - i].style.backgroundColor = "#13CE66";
//THIS IS FOR THE NEXT HALF OF THE BLOCKS.
for (let i = blocksMiddleIdx + 1; i < blocks.length - 1; i++) {
for (let j = blocksMiddleIdx + 1; j < blocks.length - i - 1; j++) {
blocks[j].style.backgroundColor = "#FF4949";
blocks[j + 1].style.backgroundColor = "#FF4949";
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
//takes the values of index and the followig index from blocks div
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]); //calling the swap function and waiting until the transition ends
blocks = document.querySelectorAll(".block");
}
//set element current index and following to different color style.
blocks[j].style.backgroundColor = "#58B7FF";
blocks[j + 1].style.backgroundColor = "#58B7FF";
}
//set the last element color to green.
blocks[blocks.length - i - 1].style.backgroundColor = "#13CE66";
//setting the operation flage to false, ending of sorting .
}
}
//reseting the operation and exit state flags.
sortingOperation = false;
exitLoop = false;
}
}
}
}
//=============================================SELECTION SORT ALGORITMH==========================================
async function selectionSort(delay = delayTime) {
if ($("input:checked").val() === "Selection Sort") {
sortingOperation = true;
if (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++) {
for (let j = 0; j < blocks.length - i - 1; j++) {
if (exitLoop === true) {
break;
} else {
blocks[j].style.backgroundColor = "#FF4949";
blocks[j + 1].style.backgroundColor = "#FF4949";
//making delay time , asyncrounsly form the main excution time.
await new Promise(resolve =>
setTimeout(() => {
resolve();
}, delay)
);
//takes the values of index and the following index from blocks div
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]); //calling the swap function and waiting until the transition ends
blocks = document.querySelectorAll(".block");
}
//play the sound effect
mySound.play();
//set element current index and following to different color style.
blocks[j].style.backgroundColor = "#58B7FF";
blocks[j + 1].style.backgroundColor = "#58B7FF";
//set the last element color to green.
blocks[blocks.length - i - 1].style.backgroundColor = "#13CE66";
//setting the operation flage to false, ending of sorting .
}
sortingOperation = false;
exitLoop = false;
}
}
}
}