forked from MastanSayyad/Visual-Sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compare.html
408 lines (368 loc) · 14.9 KB
/
Compare.html
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE html>
<html>
<head>
<title>Sorting Algorithms Comparison</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
#buttons {
margin: 20px;
text-align: center;
}
input, button, select {
margin: 5px;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
outline: none;
}
button {
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #ddd;
}
.btn-primary {
background-color: #007bff;
color: white;
border: none;
}
.btn-primary:hover {
background-color: #0056b3;
}
.btn-danger {
background-color: #dc3545;
color: white;
border: none;
}
.btn-danger:hover {
background-color: #bd2130;
}
.header {
background-color: #333;
color: white;
padding: 20px 0;
text-align: center;
}
.sh {
display: flex;
justify-content: center;
align-items: flex-end;
height: 400px;
margin-top: 20px;
width: 45%;
position: relative;
margin: 0 20px;
}
#heading {
text-align: center;
margin-top: 20px;
color: #333;
font-size: 36px;
font-weight: bold;
}
.container {
display: flex;
justify-content: space-around;
}
.bar-container {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
transition: left 0.5s ease-in-out;
}
.bar {
background-color: #3498db;
transition: background-color 0.5s ease-in-out, height 0.5s ease-in-out;
}
.bar-label {
margin-bottom: 5px;
font-weight: bold;
}
.comparing {
background-color: #e74c3c;
}
.sorted {
background-color: #2ecc71;
}
.time{
display:flex;
align-items: end;
justify-content:space-around;
margin: 10px 20px;
}
</style>
</head>
<body>
<div id="Loader"></div>
<div class="header">
<h1>Sorting Algorithms Comparison</h1>
</div>
<div id="contain">
<h2 id="heading">SORTING VISUALIZATION</h2>
<div class="container">
<div class="sh" id="visualization1"></div>
<div class="sh" id="visualization2"></div>
</div>
<div id="buttons">
<input id="array" type="text" placeholder="Enter array separated by space" size="30">
<button id="submit" class="btn-primary" onclick="submit()">Submit</button>
<select id="sortSelect1">
<option value="bubble">Bubble Sort</option>
<option value="selection">Selection Sort</option>
<option value="insertion">Insertion Sort</option>
<option value="merge">Merge Sort</option>
</select>
<select id="sortSelect2">
<option value="bubble">Bubble Sort</option>
<option value="selection">Selection Sort</option>
<option value="insertion">Insertion Sort</option>
<option value="merge">Merge Sort</option>
</select>
<button id="start" class="btn-primary" onclick="startSort()">Start Sort</button>
<button class="btn-primary" onclick="stop = true; stopClicked()">Stop</button>
<button id="resume" class="btn-primary" onclick="stop = false; startSort()">Resume</button>
<button id="reset" class="btn-primary" onclick="reset()">Reset</button>
<button class="btn-danger" onClick="window.location.reload();">Clear</button>
</div>
</div>
<script>
let array = [];
let stop = false;
const delay = ms => new Promise(res => setTimeout(res, ms));
const delayTime = 300;
function stopClicked(){
document.getElementById("resume").disabled = false;
document.getElementById("reset").disabled = false;
}
function disableSubmitButton() {
document.getElementById("submit").disabled = true;
document.getElementById("start").disabled = true;
document.getElementById("resume").disabled = true;
document.getElementById("reset").disabled = true;
}
function enableSubmitButton() {
document.getElementById("submit").disabled = false;
document.getElementById("start").disabled = false;
document.getElementById("resume").disabled = false;
document.getElementById("reset").disabled = false;
}
function submit() {
const input = document.getElementById("array").value;
array = input.split(" ").map(Number);
visualizeArray(array, 'visualization1');
visualizeArray(array, 'visualization2');
}
function visualizeArray(arr, containerId) {
const container = document.getElementById(containerId);
container.innerHTML = "";
const maxVal = Math.max(...arr);
const containerWidth = container.offsetWidth;
const barWidth = Math.max(30, Math.min(100, containerWidth / arr.length - 2));
arr.forEach((val, idx) => {
const barContainer = document.createElement("div");
barContainer.className = "bar-container";
barContainer.style.width = `${barWidth}px`;
barContainer.style.left = `${idx * (barWidth + 2)}px`;
const label = document.createElement("div");
label.className = "bar-label";
label.textContent = val;
const bar = document.createElement("div");
bar.className = "bar";
bar.style.height = `${(val / maxVal) * 300}px`;
bar.style.width = `${barWidth}px`;
bar.id = `${containerId}-bar-${idx}`;
barContainer.appendChild(label);
barContainer.appendChild(bar);
container.appendChild(barContainer);
});
}
async function updateBars(containerId) {
const maxVal = Math.max(...array);
array.forEach((val, idx) => {
const container = document.getElementById(`${containerId}-bar-${idx}`).parentElement;
const label = container.querySelector('.bar-label');
const bar = container.querySelector('.bar');
label.textContent = val;
bar.style.height = `${(val / maxVal) * 300}px`;
});
await delay(delayTime);
}
async function swap(i, j, containerId) {
const temp = array[i];
array[i] = array[j];
array[j] = temp;
const container1 = document.getElementById(`${containerId}-bar-${i}`).parentElement;
const container2 = document.getElementById(`${containerId}-bar-${j}`).parentElement;
const tempLeft = container1.style.left;
container1.style.left = container2.style.left;
container2.style.left = tempLeft;
container1.querySelector('.bar').id = `${containerId}-bar-${j}`;
container2.querySelector('.bar').id = `${containerId}-bar-${i}`;
await updateBars(containerId);
}
async function bubbleSort(containerId) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (stop) return;
document.getElementById(`${containerId}-bar-${j}`).classList.add("comparing");
document.getElementById(`${containerId}-bar-${j + 1}`).classList.add("comparing");
await delay(delayTime);
if (array[j] > array[j + 1]) {
await swap(j, j + 1, containerId);
}
document.getElementById(`${containerId}-bar-${j}`).classList.remove("comparing");
document.getElementById(`${containerId}-bar-${j + 1}`).classList.remove("comparing");
}
document.getElementById(`${containerId}-bar-${array.length - i - 1}`).classList.add("sorted");
}
}
async function selectionSort(containerId) {
for (let i = 0; i < array.length; i++) {
let minIdx = i;
document.getElementById(`${containerId}-bar-${i}`).classList.add("comparing");
for (let j = i + 1; j < array.length; j++) {
if (stop) return;
document.getElementById(`${containerId}-bar-${j}`).classList.add("comparing");
await delay(delayTime);
if (array[j] < array[minIdx]) {
minIdx = j;
}
document.getElementById(`${containerId}-bar-${j}`).classList.remove("comparing");
}
if (minIdx !== i) {
await swap(i, minIdx, containerId);
}
document.getElementById(`${containerId}-bar-${i}`).classList.remove("comparing");
document.getElementById(`${containerId}-bar-${i}`).classList.add("sorted");
}
}
async function insertionSort(containerId) {
for (let i = 1; i < array.length; i++) {
let j = i;
document.getElementById(`${containerId}-bar-${j}`).classList.add("comparing");
while (j > 0 && array[j - 1] > array[j]) {
if (stop) return;
await swap(j, j - 1, containerId);
j--;
await delay(delayTime);
}
document.getElementById(`${containerId}-bar-${j}`).classList.remove("comparing");
for (let k = 0; k <= i; k++) {
document.getElementById(`${containerId}-bar-${k}`).classList.add("sorted");
}
}
}
async function mergeSortWrapper(containerId) {
await mergeSort(0, array.length - 1, containerId);
}
async function mergeSort(left, right, containerId) {
if (left < right) {
const middle = Math.floor((left + right) / 2);
await mergeSort(left, middle, containerId);
await mergeSort(middle + 1, right, containerId);
await merge(left, middle, right, containerId);
}
}
async function merge(left, middle, right, containerId) {
const leftArray = array.slice(left, middle + 1);
const rightArray = array.slice(middle + 1, right + 1);
let i = 0, j = 0, k = left;
while (i < leftArray.length && j < rightArray.length) {
if (stop) return;
await highlightBars([left + i, middle + 1 + j], containerId, "comparing");
await delay(delayTime);
if (leftArray[i] <= rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
await updateSingleBar(k, containerId);
k++;
}
while (i < leftArray.length) {
if (stop) return;
array[k] = leftArray[i];
await updateSingleBar(k, containerId);
i++;
k++;
}
while (j < rightArray.length) {
if (stop) return;
array[k] = rightArray[j];
await updateSingleBar(k, containerId);
j++;
k++;
}
for (let i = left; i <= right; i++) {
await highlightBars([i], containerId, "sorted");
}
}
async function highlightBars(indices, containerId, className) {
indices.forEach(index => {
document.getElementById(`${containerId}-bar-${index}`).classList.remove("comparing", "sorted");
if (className) {
document.getElementById(`${containerId}-bar-${index}`).classList.add(className);
}
});
await delay(delayTime);
}
async function updateSingleBar(index, containerId) {
const maxVal = Math.max(...array);
const container = document.getElementById(`${containerId}-bar-${index}`).parentElement;
const label = container.querySelector('.bar-label');
const bar = container.querySelector('.bar');
label.textContent = array[index];
bar.style.height = `${(array[index] / maxVal) * 300}px`;
await delay(delayTime);
}
function reset() {
stop = false;
visualizeArray(array, 'visualization1');
visualizeArray(array, 'visualization2');
}
async function startSort() {
disableSubmitButton();
reset();
const sortMethod1 = document.getElementById("sortSelect1").value;
const sortMethod2 = document.getElementById("sortSelect2").value;
const sortFunc1 = getSortFunction(sortMethod1, 'visualization1');
const sortFunc2 = getSortFunction(sortMethod2, 'visualization2');
const promise1 = sortFunc1();
const promise2 = sortFunc2();
await Promise.all([promise1, promise2]);
enableSubmitButton();
}
function getSortFunction(sortMethod, containerId) {
switch(sortMethod) {
case "bubble":
return () => bubbleSort(containerId);
case "selection":
return () => selectionSort(containerId);
case "insertion":
return () => insertionSort(containerId);
case "merge":
return () => mergeSortWrapper(containerId);
}
}
</script>
<script>
var loader = document.getElementById("Loader");
window.addEventListener("load", function(){
loader.style.display = "none";
})
</script>
</body>
</html>