-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
492 lines (457 loc) · 17.1 KB
/
index.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
function bubble()
{
randomize();
$("#demo").html("<strong><a href='https://www.geeksforgeeks.org/bubble-sort/' target='_blank'>Bubble Sort</a></strong><br>Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.<br>Time Complexity : O(N<sup>2</sup>) <br>Space Complexity : O(1) <br><br> <center><button id='start' type='button' onclick='bubbleSort()' class='btn button-response btn-success' style='color: white;' >Sort</button></center>");
animate($("#demo"));
}
function insertion()
{
randomize();
$("#demo").html("<strong><a href='https://www.geeksforgeeks.org/insertion-sort/' target='_blank'>Insertion Sort</a></strong><br>Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.<br>Time Complexity : O(N<sup>2</sup>) <br>Auxiliary Space : O(1) <br><br> <center><button id='start' type='button' onclick='insertionSort()' class='btn button-response btn-success' style='color: white;' >Sort</button></center> ");
animate($("#demo"));
}
function selection()
{
randomize();
$("#demo").html("<strong><a href='https://www.geeksforgeeks.org/selection-sort/' target='_blank'>Selection Sort</a></strong><br>Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest element from the unsorted portion of the list and moving it to the sorted portion of the list. The algorithm repeatedly selects the smallest element from the unsorted portion of the list and swaps it with the first element of the unsorted portion. This process is repeated for the remaining unsorted portion of the list until the entire list is sorted.<br>Time Complexity : O(N<sup>2</sup>) <br>Auxiliary Space : O(1) <br><br> <center><button id='start' type='button' onclick='selectionSort()' class='btn button-response btn-success' style='color: white;' >Sort</button></center>");
animate($("#demo"));
}
function merge()
{
randomize();
$("#demo").html("<strong><a href='https://www.geeksforgeeks.org/merge-sort/' target='_blank'>Merge Sort</a></strong><br>Merge sort is a sorting algorithm that works by dividing an array into smaller subarrays, sorting each subarray, and then merging the sorted subarrays back together to form the final sorted array. In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each half, and then merge the sorted halves back together. This process is repeated until the entire array is sorted.<br>Time Complexity : O(NlogN) <br>Auxiliary Space : O(N) <br><br> <center><button id='start' type='button' onclick='mergeSort()' class='btn button-response btn-success' style='color: white;' >Sort</button></center> ");
animate($("#demo"));
}
function heap()
{
randomize();
$("#demo").html("<strong><a href='https://www.geeksforgeeks.org/heap-sort/' target='_blank'>Heap Sort</a></strong><br>Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.<br>Time Complexity : O(NlogN) <br>Auxiliary Space : O(1) <br><br> <center><button id='start' type='button' onclick='heapSort()' class='btn button-response btn-success' style='color: white;' >Sort</button></center> ");
animate($("#demo"));
}
function quick()
{
randomize();
$("#demo").html("<strong><a href='https://www.geeksforgeeks.org/quick-sort/' target='_blank'>Quick Sort</a></strong><br> QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.<br>Time Complexity : O(NlogN) <br>Auxiliary Space : O(NlogN) <br><br> <center><button id='start' type='button' onclick='quickSort()' class='btn button-response btn-success' style='color: white;' >Sort</button></center> ");
animate($("#demo"));
}
var flag = false ;
var ArrayLength = document.querySelectorAll(".container-bar").length;
var array = [] ;
var indices=[];
var delay = 2 ;
var functionNumber = 0;
const randomButton = document.getElementById("randomButton");
var barContainerWidth = window.getComputedStyle(document.querySelector(".bar-container")).width;
var barContainerHeight = window.getComputedStyle(document.querySelector(".bar-container")).height;
barContainerHeight = barContainerHeight.replace(/px/g,"");
barContainerWidth = barContainerWidth.replace(/px/g,"");
const typed = new Typed(".sort", {
strings: ["Sorting Visualizer"],
typeSpeed: 100,
loop: false,
});
function randomize()
{
if( flag===false )
{
array = [] ;
for(var i=0; i<ArrayLength ; i++)
{
barContainerWidth = window.getComputedStyle(document.querySelector(".bar-container")).width;
barContainerHeight = window.getComputedStyle(document.querySelector(".bar-container")).height;
barContainerHeight = barContainerHeight.replace(/px/g,"");
barContainerWidth = barContainerWidth.replace(/px/g,"");
var newHeight = Math.ceil(Math.random()*parseFloat(barContainerHeight));
array.push(newHeight);
newHeight+="px";
var element = document.querySelectorAll(".container-bar")[i];
element.style.height = newHeight;
var newWidth=parseFloat(barContainerWidth)/((2*ArrayLength)-1);
element.style.width=newWidth+"px";
element.style.left=(2*i*newWidth)+"px";
element.classList.remove("final");
}
}
}
function sleep(ms)
{
return new Promise(resolve => setTimeout(resolve, ms));
}
async function bubbleSort()
{
if( flag===false )
{
flag=true;
randomButton.classList.add("danger");
var start = document.querySelector("#start");
start.classList.add("danger");
functionNumber = 1 ;
var randomFlag;
for(var i=0;i<ArrayLength-1;i++)
{
randomFlag=false;
for (var j = 0; j < ArrayLength-i-1; j++)
{
if (array[j] > array[j+1])
{
animateBar(document.querySelectorAll(".container-bar")[j]);
animateBar(document.querySelectorAll(".container-bar")[j+1]);
await sleep(delay);
randomFlag=true;
var temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
reAssignRed();
}
}
if( randomFlag===false )
{
break;
}
}
reAssign();
flag=false;
}
}
async function insertionSort()
{
if( flag===false )
{
flag=true;
randomButton.classList.add("danger");
var start = document.querySelector("#start");
start.classList.add("danger");
functionNumber = 1 ;
var i, j , n=ArrayLength;
for( i=1 ; i < n ; i++ )
{
j=i;
while( j>0 && array[j-1]>array[j] )
{
animateBar(document.querySelectorAll(".container-bar")[j-1]);
animateBar(document.querySelectorAll(".container-bar")[j]);
await sleep(delay);
var temp=array[j-1];
array[j-1]=array[j];
array[j]=temp;
reAssignRed();
j--;
}
}
reAssign();
flag=false;
}
}
async function selectionSort()
{
if( flag===false )
{
flag=true;
randomButton.classList.add("danger");
var start = document.querySelector("#start");
start.classList.add("danger");
var i, j, min_idx , n=ArrayLength;
for (i = 0; i < n-1; i++)
{
functionNumber = 3 ;
min_idx = i;
for (j = i+1 ; j < n; j++)
{
animateBar(document.querySelectorAll(".container-bar")[min_idx]);
if (array[j] < array[min_idx])
{
min_idx = j;
animateBar(document.querySelectorAll(".container-bar")[min_idx]);
await sleep(delay);
}
}
functionNumber = 1 ;
if( min_idx !== i )
{
animateBar(document.querySelectorAll(".container-bar")[i]);
animateBar(document.querySelectorAll(".container-bar")[min_idx]);
await sleep(delay);
var temp=array[i];
array[i]=array[min_idx];
array[min_idx]=temp;
reAssignRed();
}
}
reAssign();
flag=false;
}
}
function mergesortindices(array,s,e)
{
if(s<e)
{
var m=Math.floor((s+e)/2);
mergesortindices(array,s,m);
mergesortindices(array,m+1,e);
indices.push(s);
indices.push(m);
indices.push(e);
}
}
async function MergeSort(array,n)
{
indices=[];
mergesortindices(array,0,n-1);
var start,mid,end;
for(let index=0 ; index<indices.length ; )
{
start=indices[index++];
mid=indices[index++];
end=indices[index++];
var n1 = mid - start + 1;
var n2 = end - mid ;
var L = [] ;
var R = [] ;
for (i = 0; i < n1; i++)
{
L.push(array[start + i]);
}
for (j = 0; j < n2; j++)
{
R.push(array[mid + 1+ j]);
}
i = 0;
j = 0;
k = start;
while (i < n1 && j < n2)
{
animateBar(document.querySelectorAll(".container-bar")[i+start]);
animateBar(document.querySelectorAll(".container-bar")[j+mid+1]);
await sleep(delay);
if (L[i] <= R[j])
{
array[k] = L[i];
i++;
}
else
{
array[k] = R[j];
j++;
}
reAssignRed();
k++;
}
while (i < n1)
{
animateBar(document.querySelectorAll(".container-bar")[i+start]);
array[k] = L[i];
await sleep(delay);
reAssignRed();
i++;
k++;
}
while (j < n2)
{
animateBar(document.querySelectorAll(".container-bar")[j+mid+1]);
array[k] = R[j];
await sleep(delay);
reAssignRed();
j++;
k++;
}
}
}
function mergeSort()
{
if( flag===false )
{
flag=true;
randomButton.classList.add("danger");
var start = document.querySelector("#start");
start.classList.add("danger");
functionNumber = 2 ;
// const startTime = performance.now();
MergeSort(array,ArrayLength).then(() => {
// const endTime = performance.now();
// const executionTime = endTime - startTime;
// period=executionTime;
// console.log(`Execution time: ${executionTime} milliseconds`);
reAssign();
flag=false;
});
}
}
async function HeapSort(array,n)
{
for (let i = 1; i < n; i++)
{
if (array[i] > array[Math.floor((i - 1) / 2)])
{
let j = i;
while (array[j] > array[Math.floor((j - 1) / 2)])
{
animateBar(document.querySelectorAll(".container-bar")[j]);
animateBar(document.querySelectorAll(".container-bar")[Math.floor((j-1)/2)]);
await sleep(delay);
var temp=array[j];
array[j]=array[Math.floor((j-1)/2)];
array[Math.floor((j-1)/2)]=temp;
reAssignRed();
j = Math.floor((j - 1) / 2);
}
}
}
for (var i = n - 1; i > 0; i--)
{
animateBar(document.querySelectorAll(".container-bar")[i]);
animateBar(document.querySelectorAll(".container-bar")[0]);
await sleep(delay);
var temp=array[0];
array[0]=array[i];
array[i]=temp;
reAssignRed();
var j = 0, index;
do
{
index = (2 * j + 1);
if (array[index] < array[index + 1] && index < (i - 1))
{
index++;
}
if (array[j] < array[index] && index < i)
{
animateBar(document.querySelectorAll(".container-bar")[j]);
animateBar(document.querySelectorAll(".container-bar")[index]);
await sleep(delay);
temp=array[j];
array[j]=array[index];
array[index]=temp;
reAssignRed();
}
j = index;
}
while (index < i);
}
}
function heapSort()
{
if( flag===false )
{
flag=true;
randomButton.classList.add("danger");
var start = document.querySelector("#start");
start.classList.add("danger");
functionNumber = 2 ;
// const startTime = performance.now();
HeapSort(array,ArrayLength).then(() => {
// const endTime = performance.now();
// const executionTime = endTime - startTime;
// period=executionTime;
// console.log(`Execution time: ${executionTime} milliseconds`);
reAssign();
flag=false;
});
}
}
async function quickSortIterative(array,l,h)
{
var stack = new Array(h - l + 1);
var top = -1;
stack[++top] = l;
stack[++top] = h;
while (top >= 0)
{
h = stack[top--];
l = stack[top--];
var p ;
var x = array[h];
var i = (l - 1);
for (var j = l; j <= h - 1; j++)
{
if (array[j] <= x)
{
i++;
animateBar(document.querySelectorAll(".container-bar")[h]);
animateBar(document.querySelectorAll(".container-bar")[i]);
animateBar(document.querySelectorAll(".container-bar")[j]);
var temp=array[i];
array[i]=array[j];
array[j]=temp;
reAssignRed();
}
await sleep(delay);
}
animateBar(document.querySelectorAll(".container-bar")[i+1]);
animateBar(document.querySelectorAll(".container-bar")[h]);
await sleep(delay);
var temp=array[i+1];
array[i+1]=array[h];
array[h]=temp;
reAssignRed();
p=i+1;
if (p + 1 < h) {
stack[++top] = p + 1;
stack[++top] = h;
}
if (p - 1 > l)
{
stack[++top] = l;
stack[++top] = p - 1;
}
}
}
function quickSort()
{
if( flag===false )
{
flag=true;
randomButton.classList.add("danger");
var start = document.querySelector("#start");
start.classList.add("danger");
functionNumber = 2;
// const startTime = performance.now();
quickSortIterative(array,0,ArrayLength-1).then(() => {
// const endTime = performance.now();
// const executionTime = endTime - startTime;
// period=executionTime;
// console.log(`Execution time: ${executionTime} milliseconds`);
reAssign();
flag=false;
});
}
}
async function reAssign()
{
for(var i=0 ; i<ArrayLength ; i++ )
{
var sortHeight=array[i];
sortHeight+="px";
var element = document.querySelectorAll(".container-bar")[i];
element.style.height=sortHeight;
element.classList.add("final");
await sleep(1);
}
randomButton.classList.remove("danger");
var start = document.querySelector("#start");
start.classList.remove("danger");
}
function reAssignRed()
{
for(var i=0 ; i<ArrayLength ; i++ )
{
var sortHeight=array[i];
sortHeight+="px";
var element = document.querySelectorAll(".container-bar")[i];
element.style.height=sortHeight;
}
}
function animate(element)
{
//element.slideUp(0).slideDown(1000);
element.fadeOut(0).fadeIn(1000);
}
function animateBar(element)
{
if(functionNumber === 1 ) delay = 1 ;
else if(functionNumber === 2) delay = 10 ;
else delay = 100 ;
element.classList.add("container-bar-intermediate");
setTimeout(function(){
element.classList.remove("container-bar-intermediate")
},delay);
}