-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScripts.js
1722 lines (1360 loc) · 63.2 KB
/
Scripts.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var sortSelector = document.getElementById("sortSelector"); //The dropdown menu where you can select what type of sort
var nodes = document.getElementsByClassName("Node"); //An array of Nodes to be sorted
const numNodes = nodes.length;
var wait; //the variable that will store all setInterval() objects
var delay = 100; //stores how many milliseconds each animation is delayed by
var randomizeButton = document.getElementById("radomize");
//The following are meant to help the nextStep button
var started = false; //for help with the "Next Step" button. Determines whether the function needs to setup the arrays or can proceed to the next step
var nextStepOrder; //the array that wil be used to store the next current order
var done = false; //Checks if the array is done or not
var stepNum = 1; //Which step we are currently on
var orderArray; //Array of the Nodes in their order by size
//order of node classes (generic, size, color)
//Changing the size class changes how high a node is.
//Changing the color class changes the color
//Function for the randomizer button
function randomize(){
let intList = []; //stores intergers from - to numNodes -1
let classes = []; //stores the classes in order for the first node to the last
resetArena();
for(let i = 0; i < numNodes; i++){ //we have now created a list of numbers from 0 to numNodes -1 inclusive
intList.push(i);
}
for(let i = numNodes - 1; i >= 0; i--){
let index = Math.floor(Math.random() * i); //chooses a random number between 0 and i
let randomNumber = intList[index]; //selects the number
let size = nodes[randomNumber].classList[1]; //selects the size from the chosen element
classes.push(size);
intList[index] = intList[i]; //moves the last elment into the spot of the element just chosen
}
//classes array now contains size classes in random order.
for(let j = numNodes - 1; j >= 0; j--){
let node = nodes[j];
let size = classes[j];
setClass(node, 1, size);
}
}
//Allows two Nodes to Be Swapped when clicked
var selected = false; //stores whether or not a node is currently selected
var selectedNode; //stores the node currently selected
for(let i = 0; i < numNodes; i++){
nodes[i].addEventListener('mousedown', manualswitchNodes);
}
function manualswitchNodes(e){
if(!selected){ //no node is selected, so select the one just clicked
resetArena();
selectedNode = this;
setClass(this, 2, "Selected");
selected = true;
}else{
selected = false;
if(this == selectedNode){ //reset it to default if this was the node just clicked
setClass(this, 2, "Default");
}else{
switchNodes(this, selectedNode);
setClass(selectedNode, 2, "Default"); //set the old node to default color
}
selctedNode = null;
}
}
//The folowing Function will Direct the algorithim to which sort is being used
//sets up the sorting variables
function startSort(){
sortSelector.disabled = true; //freezes the sortSelector
orderArray = getOrder(); //sets selectionOrder to be the current order of the nodes
started = true;
stepNum = 1; //reset the number of steps
if(selected){setClass(selectedNode, 2, "Default");} //if a node is selected for manual switching, set it to default
selected = false; //remove the selected Section for the Node
}
//use of the sort button
function findSort(){
if(!started){
startSort();
}
if(done)
return;
randomizeButton.disabled = true;
let decision = sortSelector.value;
switch(decision){
case "Selection":
selectionSort();
break;
case "Quick":
quicksort();
break;
case "Bubble":
bubblesort();
break;
case "Insertion":
insertionSort();
break;
case "Merge":
mergeSort();
break;
case "Heap":
heapSort();
break;
case "Bogo":
bogoSort();
break;
}
}
//for the "next step" button
function findStep(){
if(!started){
startSort();
}
let decision = sortSelector.value;
if(!done){
switch(decision){
case "Selection":
runSelectionSteps();
break;
case "Quick":
runQuickSteps();
break;
case "Bubble":
runBubbleStep();
break;
case "Insertion":
runInsertionStep();
break;
case "Merge":
runMergeStep();
break;
case "Heap":
runHeapStep();
break;
case "Bogo":
runBogoStep();
break;
}
}
}
//Selection Sort Function
var selectionCurrent = 0; //how many nodes in the array are already sorted
var selectionSmallest; //index of the smalles unsorted node so far
var selectionIndex; //index of the Node being compared to the smallest Index
var sortedSwitchStep = 1; //Switching 2 nodes takes 3 steps. This stores which one we are on.
var foundNewSorted = false; //stores whether we are supposed to change the smallest index on this step
var selectionStarted = false; //have we set up the variables needed for selection sort?
//function to get the arena set up for selection sorting
function startSelection(){
sortedSwitchStep = 1;
selectionStarted = true;
selectionCurrent = 0; //reset the number of sorted so that we can use it again without refreshing
selectionIndex = 1;
selectionSmallest = 0;
addStep("Node 1 is the first unsorted Array. Set Node 1 to \"smallest\".");
setClass(nodes[0], 2, "Combined");
foundNewSorted = false;
}
function selectionSort(){ //this is run when the "sort" button is pressed for selection sort
if(!selectionStarted){ //if the next step button has already been pressed, we don't need to run the startSelection function
startSelection();
}
wait = setInterval(selectionStep, delay); //calls each selection with a .5 second delay
}
function runSelectionSteps(){ //meant for use with the Next Step Button
if(!selectionStarted){
startSelection();
}else{
if(!done){
selectionStep();
}
}
}
function selectionStep(){
if(foundNewSorted){ //this step, all we are doing is changing the index of the sorted Node
selectionChangeSmallest();
return;
}
//change the color of the previous if need be
if(selectionIndex - 1 != selectionCurrent){ //if the one before is the first unsorted node, do nothing
selectionIncrementColors();
}
if(selectionIndex < numNodes){ //checks every node after this index. If a smaller node is found, change smallest
selectionCompareNodes();
return; //do not run the switch if just finished iterating to find the smallest
}
//smallest is now guarenteed to be the smallest unsorted node
if(sortedSwitchStep == 1){ //switch the sizes and colors of the smallest and the first unsored node
selectionSwap();
}else if(sortedSwitchStep == 2){ //sets the current node to "sorted" color
selectionSetSorted();
}else if(sortedSwitchStep == 3){
if(selectionCurrent >= (numNodes - 2)){ //the nodes are now sorted
selectionFinish();
return;
}
selectionChangeCurrent();
}else if(sortedSwitchStep == 4){
selectionSetNextSmallest();
}
}
/*
Selection sort works by iterating through each element, and swaping the smallest element with the first element that isn't yet sorted.
Classifications:
Current: "Current" The first unsorted Node
Smallest" "Special" The smallest unsorted node found so far
Index: "Index" The Node we are comparing with the Node set to smallest
Sorted: "Sorted" Nodes at the begining of the list we know are sorted
First, we set the first node as both current and smallest. We then iterate through each node in the list. If we find a smaller Node,
than the "smallest" node, we set that to "smallest".
At the end, switch the current Node and the smallet Node
Repeat for each Node
*/
//Changes the index to match the smallest function
function selectionChangeSmallest(){
addStep("Index Node " + (selectionIndex + 1) + " is smaller than smallest node " + (selectionSmallest + 1) + ". Set Node " + (selectionIndex + 1) + " to \"smallest\"");
setClass(nodes[selectionIndex], 2, "Selected"); //changing the current node to a blend of index and smallest (selected).
if(selectionSmallest == selectionCurrent){ //the previous smallest node is the first unsorted node
setClass(nodes[selectionSmallest], 2, "Current");
}else{
setClass(nodes[selectionSmallest], 2, "Default");
}
selectionSmallest = selectionIndex;
foundNewSorted = false;
selectionIndex++;
}
//moves each color up
function selectionIncrementColors(){
if(selectionIndex - 1 == selectionSmallest){
setClass(nodes[selectionIndex - 1], 2, "Special"); //if the previous class is the current smallest, set it to the smallest color
}else{
setClass(nodes[selectionIndex - 1], 2, "Default"); //otherwise, set to default color
}
}
//Compare the index and smallest nodes
function selectionCompareNodes(){
//adding the step to the step display
addStep("Compare index Node " + (selectionIndex + 1) + " to smallest Node " + (selectionSmallest + 1) +".");
setClass(nodes[selectionIndex], 2, "Index"); //set the index node to the index color
//if index is smaller than smallest
if(orderArray[selectionIndex] < orderArray[selectionSmallest]){
foundNewSorted = true;
return;
}
selectionIndex++;
}
//switches the current node and the smallest node after iterating thorugh each node after the current
function selectionSwap(){
swapArray(orderArray, selectionSmallest, selectionCurrent);
numSwap(selectionSmallest, selectionCurrent); //switch the sizes and colors of the smallest and the first unsored node
sortedSwitchStep++;
addStep("Switch current Node " + (selectionCurrent + 1) + " and smallest Node " + (selectionSmallest + 1) + ".");
}
//sets the current node to be a sorted color
function selectionSetSorted(){
setClass(nodes[selectionSmallest], 2, "Default");
setClass(nodes[selectionCurrent], 2, "Sorted"); //sets the current node to "sorted" color
addStep("Current Node " + (selectionCurrent + 1) + " is now sorted.");
sortedSwitchStep++;
}
//set the node after the current node as current
function selectionChangeCurrent(){
selectionCurrent++; //increments the number of sorted nodes
selectionSmallest = selectionCurrent; //Set smallest to be the index of the smalles non sorted node
setClass(nodes[selectionSmallest], 2, "Current"); //changes the color of the next to the "current" color
addStep("Set Node " + (selectionSmallest + 1) + " as the current node");
sortedSwitchStep++;
}
//set the new current node as smallest
function selectionSetNextSmallest(){
addStep("Set Node " + (selectionSmallest + 1) + " as the smallest node");
setClass(nodes[selectionSmallest], 2, "Combined"); //changes the color of the next to the "current" and "smallest" color
selectionIndex = selectionCurrent + 1;
sortedSwitchStep = 1;
}
function selectionFinish(){
setClass(nodes[(numNodes - 1)], 2, "Sorted"); //sets final array to sorted color
clearInterval(wait); //prevent the loop from continuing on sort button
randomizeButton.disabled = false; //enables the randomize button
done = true; //causes next step button to do nothing
sortSelector.disabled = false; //allows for the selection of new sorting types
addStep("Node " + (numNodes) + " is now sorted");
addStep("All Nodes are now sorted");
}
/*
BubbleSort
The largest remaining Node "bubble" to the top, until each node is done
Classifications and their classes:
Current = "Current". The Current NOde
Next = "Special" The Node we are comparing current to, and the node immidealty after current
Sorted = "Sorted" Sorted Nodes at the end of the lsit
We will Start by setting Node "1" as Current and Node 2. as Next.
We will increment both current and next until we hit the end of the unsorted Nodes, switching the nodes if Current is larger than Next.
Reset Node 1 as Current and Node 2 as Next
Once Each Node has been moved to the end, or we go through a run without swapping any noes, the list is sorted.
*/
var bubbleLimit = numNodes; //stores the limit of how many nodes we need to check on each run (how many unsorted Nodes)
var bubbleIndex = 0; //stores the index Node's index
var bubbleSwap = false; //does the next step involve switching?
var bubbleStarted = false; //Have the bubble sort variables been set to their needs?
var bubbleIsSorted = true; //Start by assuming each iteration that the list is sorted. If a switch is made, set this to false
var bubbleEndStep = 1; //Once a node reaches the end, several steps must be taken
function bubbleStart(){
bubbleLimit = numNodes;
bubbleIndex = 0;
bubbleStarted = true;
bubbleIsSorted = true;
addStep("Set node 1 as current and node 2 as \"next\"");
}
//this function fires when the "Next Step" button is pressed
function runBubbleStep(){
if(!bubbleStarted){
bubbleStart();
}
bubbleStep();
}
//to be run when the button "sort" is pressed
function bubblesort(){
if(!bubbleStarted){
bubbleStart();
}
wait = setInterval(bubbleStep, delay);
}
function bubbleStep(){
//Bubble sort is done
if(bubbleLimit == 1){
bubbleLateEnd();
return;
}
//switch two Nodes
if(bubbleSwap){
bubbleSwapNodes();
return;
}
if(bubbleIndex < bubbleLimit - 1){ //Compare Nodes to the next one
bubbleCompareNodes();
}else{ //The largest remaining node is at the end
if(bubbleEndStep == 1){ //Set The colors of the end of the line
bubbleSetSorted();
}else if(bubbleEndStep == 2){ //set the colors at the start of the line
bubbleNextRun();
}
}
}
//swaps two nodes
function bubbleSwapNodes(){
addStep("Current Node " + (bubbleIndex + 1) + " is larger than next Node " + (bubbleIndex + 2) + ". Switch the two nodes.");
swapArray(orderArray, bubbleIndex, bubbleIndex + 1);
bubbleSwap = false;
numSwap(bubbleIndex, bubbleIndex + 1);
bubbleIndex++;
bubbleIsSorted = false; //If a single switch occurs, that means the list is not sorted
bubbleSwapStep++;
if(bubbleIndex >= bubbleLimit - 1){ // do not set the next node if we are on the last node
bubbleSwap = false;
bubbleSwapStep = 1;
}
}
//moves the index and current nodes up, then compares them
function bubbleCompareNodes(){
addStep("Compare current Node " + (bubbleIndex + 1) + " next Node " + (bubbleIndex + 2) + ".");
if(bubbleIndex > 0){
setClass(nodes[bubbleIndex - 1], 2, "Default");
}
setClass(nodes[bubbleIndex], 2, "Current");
setClass(nodes[bubbleIndex + 1], 2, "Special");
if(orderArray[bubbleIndex] > orderArray[bubbleIndex + 1]){
bubbleSwap = true;
return;
}
bubbleIndex++;
}
//changing the colors of the largest unsorted node once it's at the end
function bubbleSetSorted(){
addStep("Node " + (bubbleIndex + 1) + " is now sorted.");
if(bubbleIsSorted){
bubbleEarlyEnd();
return;
}
setClass(nodes[bubbleIndex], 2, "Sorted"); //set node to sorted color
setClass(nodes[bubbleIndex - 1], 2, "Default"); //set previous node to default
bubbleEndStep++;
bubbleLimit--;
}
//Sets the index and next nodes for the next run
function bubbleNextRun(){
addStep("Set node 1 as current and node 2 as \"next\"");
setClass(nodes[0], 2, "Current");
setClass(nodes[1], 2, "Special");
bubbleIndex = 0;
bubbleIsSorted = true;
bubbleEndStep = 1;
}
//if we go through the list without swapping, the list is sorted. We can trigger the "early end" event in that case.
function bubbleEarlyEnd(){
addStep("No Swaps were made. The List is now sorted.");
for(q = 0; q < bubbleLimit; q++){
setClass(nodes[q], 2, "Sorted");//set all remaining nodes as sorted
}
bubbleEnd();
}
//We have sorted every node from 2- numNodes, so the list now must be sorted
function bubbleLateEnd(){
bubbleEnd();
addStep("The list is now sorted.");
setClass(nodes[0], 2, "Sorted"); //set the first node to be the sorted color
randomizeButton.disabled = false;
return;
}
function bubbleEnd(){
randomizeButton.disabled = false;
clearInterval(wait);
sortSelector.disabled = false;
done = true;
}
/*
Insertion sort works by taking an already sorted array at the start (An array of One is sorted),
and swapping the next element with the next largest sorted element until the next element is in the proper order.
Repeat for each element.
Classifications: Class
Semi-Sorted: "Sorted". Nodes at the begining of the list are sorted compared to each other.
Current: "Current" The Node currently we are trying to insert into the semi-sorted list
We start by setting the first node as semi-sorted, as a list with just one node is sorted.
Then we set the second node as Current, and the first node as prev. We try to insert it into the semi-sorted list, then we repeat until
We've done it for every lsit
Each time we iterate through the lists and take the first unsorted Node, swapping it with the previous
semi-sorted Node until we hit the begining of the list or a node smaller than it. The current Node is now semi-sorted.
Repeat for each Node.
*/
var insertionBeyond = 0; //Index of the first untouched Node
var insertionRunner; //Index of the node as it's being inserted to the right place
var insertionSwap = false; //Whether we are swapping this step or not
var insertionStarted = false; //Have we started insertion sort yet?
var insertionSwapStep = 1;
function insertionStart(){
insertionStarted = true;
insertionSwap = false;
insertionBeyond = 1;
addStep("Node 1 is now semi-sorted.");
setClass(nodes[0], 2, "Sorted"); // Set Node 1 to sorted
}
//runs when the sort button is pressed
function insertionSort(){
if(!insertionStarted){
insertionStart();
}
orderArray = getOrder();
wait = setInterval(insertionStep, delay);
}
//runs when the next step button is pressed
function runInsertionStep(){
if(!insertionStarted){
insertionStart();
}else{
insertionStep();
}
}
function insertionStep(){
if(insertionSwap){
//we have hit the end or a smaller node
if((insertionRunner <= 0 || orderArray[insertionRunner] >= orderArray[insertionRunner - 1]) && (insertionSwapStep == 1)){ //run the final step to set the new one
insertionSetSorted();
return;
}
if(insertionSwapStep == 1){
insertionSwapNodes();
}else if(insertionSwapStep == 2){
insertionNewPrev();
}
return;
}
insertionNewRunner();
}
function insertionSetSorted(){
insertionSwap = false;
insertionSwapStep = 1;
insertionBeyond++;
setClass(nodes[insertionRunner], 2, "Sorted"); //set this node as the sorted color now (refered to as semisorted);
if(insertionRunner < insertionBeyond - 1){
setClass(nodes[insertionRunner + 1], 2, "Sorted"); //set the old previous node as sorted
}
if(insertionRunner > 0){ //set the previous node as sorted
setClass(nodes[insertionRunner - 1], 2, "Sorted");
}
addStep("Current Node " + (insertionRunner + 1) + " is now semi-sorted.");
//have we have sorted the entire array
if(insertionBeyond == numNodes){
addStep("The array is now sorted.");
done = true;
clearInterval(wait);
sortSelector.disabled = false;
randomizeButton.disabled = false;
return;
}
}
function insertionSwapNodes(){
addStep("Current Node " + (insertionRunner + 1) + " is less than previous node " + insertionRunner + ". Swap the two nodes.");
swapArray(orderArray, insertionRunner, insertionRunner - 1);
numSwap(insertionRunner, insertionRunner - 1); //swap the array with the previous
insertionSwapStep++;
insertionRunner--;
if(insertionRunner == 0){
insertionSwapStep = 1;
}
}
function insertionNewPrev(){
setClass(nodes[insertionRunner + 1], 2, "Sorted"); //set the old prev as sorted
setClass(nodes[insertionRunner], 2, "Current"); //set the new current the correct color
setClass(nodes[insertionRunner-1], 2, "Special"); //set the new previous as previous color
insertionSwapStep = 1;
addStep("Set Node " + insertionRunner + " as \"prev\".");
}
function insertionNewRunner(){
insertionRunner = insertionBeyond; //the next node to enter the sorted array is the first unsorted node
insertionSwap = true; //next step is to start swapping
setClass(nodes[insertionBeyond], 2, "Current"); //give the current node the current color
setClass(nodes[insertionBeyond - 1], 2, "Special");
addStep("Set Node " + (insertionRunner + 1) + " as \"current\" and Node " + (insertionRunner + 2) + " as \"prev\".");
}
//BogoSort
//BogoSort
//BogoSort
//BogoSort
//BogoSort
//BogoSort
//BogoSort
//BogoSort
var bogoIsSorted; //Boolean if this is sorted or not. Always Assumed to be true
var bogoIndex; //which node are we comparing to the next?
var bogoStarted = false;
function startBogo(){
bogoIsSorted = true; //start by assuming the array is sorted
bogoIndex = 0; //begin the array at one
orderArray = getOrder(); //get the array to match the nodes
addStep("Set Node 1 as \"current\" and Node 2 as \"next\"");
bogoStarted = true;
}
function runBogoStep(){
if(!bogoStarted){
startBogo();
}
bogoSteps();
}
function bogoSort(){
if(!bogoStarted){
startBogo();
}
wait = setInterval(bogoSteps, delay);
}
function bogoSteps(){
//Compare Nodes
if(bogoIsSorted && bogoIndex < numNodes - 1){
bogoCompare();
return;
}
//we finished the iteration and found no problems. The list is sorted
if(bogoIsSorted){
bogoFinish();
return;
}
//We found irregularities
addStep("The List is not sorted. Shuffle the list");
bogoRandomize();
bogoIsSorted = true;
bogoIndex = 0;
orderArray = getOrder();
}
function bogoCompare(){
setClass(nodes[bogoIndex], 2, "Current"); //set the current node to current color
setClass(nodes[bogoIndex + 1], 2, "Special"); //set the next node as the next color
if(bogoIndex > 0){
setClass(nodes[bogoIndex - 1], 2, "Sorted"); //set the previous node to the default color
}
addStep("Compare current Node " + (bogoIndex + 1) + " to next Node " + (bogoIndex + 2) + ".");
if(orderArray[bogoIndex] > orderArray[bogoIndex + 1]){
//The array is not in order
bogoIsSorted = false;
}
bogoIndex++;
}
function bogoFinish(){
setClass(nodes[numNodes - 1], 2, "Sorted"); //Set the last two nodes to be the sorted color
setClass(nodes[numNodes - 2], 2, "Sorted"); //
addStep("Nothing is out of order. The list is now sorted");
done = true;
clearInterval(wait);
sortSelector.disabled = false;
randomizeButton.disabled = false;
}
function bogoRandomize(){
let intList = []; //stores intergers from - to numNodes -1
let classes = []; //stores the classes in order for the first node to the last
for(let q = 0; q <= bogoIndex + 1; q++){
setClass(nodes[q], 2, "Default"); //Set all nodes to the default color
}
for(let i = 0; i < numNodes; i++){ //we have now created a list of numbers from 0 to numNodes -1 inclusive
intList.push(i);
}
for(let i = numNodes - 1; i >= 0; i--){
let index = Math.floor(Math.random() * i); //chooses a random number between 0 and i
let randomNumber = intList[index]; //selects the number
let size = nodes[randomNumber].classList[1]; //selects the size from the chosen element
classes.push(size);
intList[index] = intList[i]; //moves the last elment into the spot of the element just chosen
}
//classes array now contains size classes in random order.
for(let j = numNodes - 1; j >= 0; j--){
let node = nodes[j];
let size = classes[j];
setClass(node, 1, size);
}
}
/*
Quick sort works by setting the end element as a "pivot" and all Nodes as "Relevant" and then seperating the list into two sublists,
one larger than the pivot, and one smaller.
Then switch the pivot with the first larger array, then that Node is now sorted.
We then do a mini sort with each sublist, setting the last element of each sublist as "pivot" and each other element as "relevant",
then seperating them like before, creating up to 2 more sublists.
Repeat for each subgroup until each subgroup is only one or zero element large, and we have a sorted list.
Pivot = "Current" Nodes are divided based on whether they are larger or smaller than this Node
Relevant ="Relevant" Nodes we are comparing with node in this particular minisubsort.
Index = "Index" The node we are currently comparing to the pivot
Smaller = "Combined" Nodes smaller than the pivot
Larger = "Special" Nodes larger than the pivot
*/
var quickSmallerIndex; //The index the smaller Node will be placed in
var quickLargerIndex; //the number of Nodes larger than the pivot
var quickStart; //Stores the begining of this particular quick sort (quicksort is recursive)
var quickEnd; //One above the end of the particular quicksort
var quickStarted = false; //Has quicksort started?
var quickPivot; //stores the pivot height
var quickDonePartition; //If false, continue partitioning. If true, set a new mini quicksort
var partitionStep; //partitioning takes 2 steps each
var quickLeftIndex; //stores the start of the mini-quicksort
var quickRightIndex; //stores the end of the mini-quicksort
var quickEndsArray = []; //Since I can't use recursion, I'll have a stack that stores each end of each sub-quicksort
function quickSetup(){
quickStart = 0;
partitionStep = 1;
quickEnd = numNodes;
let endStart = [0, numNodes];
quickEndsArray.push(endStart);
orderArray = getOrder();
quickDonePartition = true;
quickStarted = true;
}
function runQuickSteps(){
if(!quickStarted){
quickSetup();
}
quickStep();
}
function quicksort(){
quickSetup();
wait = setInterval(quickStep, delay);
}
function quickStep(){
//We must Set Nodes to relevant and get the pivot
if(quickDonePartition){
setMiniQuicksort();
return;
}
//move a node either in the larger or smaller pile
if(quickSmallerIndex < quickLargerIndex){
quickPartition();
return;
}
movePivot();
//The stack is empty. No more semisorts are needed
if(quickEndsArray.length == 0){
quickSortEnd();
}
}
//Sets up the function for a recurivsie quicksort
function setMiniQuicksort(){
let endStart = quickEndsArray.pop(); //get the new start and end from the stack
quickLeftIndex = endStart[0];
quickSmallerIndex = endStart[0]; //set the start of the semisort
quickRightIndex = endStart[1];
quickLargerIndex = endStart[1] - 1; //set the end of the semisort
quickPivot = orderArray[quickLargerIndex]; //Set the pivot as the last index
//there is only one element in this subarray. Just set it to sorted
if(quickSmallerIndex == quickLargerIndex){
addStep("Node " + (quickLargerIndex + 1) + " is now sorted");
setClass(nodes[quickLargerIndex], 2, "Sorted");
//The stack is empty. No more semisorts are needed. Check if the list is sorted
if(quickEndsArray.length == 0){
quickSortEnd();
}
return;
}
addStep("Set Node " + (quickLargerIndex + 1) + " as \"pivot\" and Nodes " + (quickSmallerIndex + 1) + " to " + (quickLargerIndex) + " as \"relevant\".");
setClass(nodes[quickLargerIndex], 2, "Current"); //Set the last node of the sublist as the pivot
for(let q = quickSmallerIndex; q < quickLargerIndex; q++){ //set every other Node of the sublist as relevant
setClass(nodes[q], 2, "Relevant");
}
quickDonePartition = false;
}
function movePivot(){
numSwap(quickRightIndex - 1, quickLargerIndex); //swap the pivot (last item in the sublist) with the first larger element
swapArray(orderArray, quickRightIndex - 1, quickLargerIndex);
addStep("The Partitioning is done. Swap pivot node " + quickRightIndex + " with first larger node " + (quickLargerIndex + 1) + ". The new Node " + (quickLargerIndex + 1) + " is now sorted.");
setClass(nodes[quickLargerIndex], 2, "Sorted"); //Set the pivot as sorted in it's new position
//add the coordinates for a new semisort on the right, from the one after the pivot to the end if any nodes ended up on the right
if(quickSmallerIndex + 1 != quickRightIndex){
let rightEndStart = [quickSmallerIndex + 1, quickRightIndex];
quickEndsArray.push(rightEndStart);
}
//add the coordinates for a new semisort of the left nodes, from this sort's begining to the pivot if any nodes ended up on the left
if(quickSmallerIndex != quickLeftIndex){
let leftEndStart = [quickLeftIndex, quickSmallerIndex];
quickEndsArray.push(leftEndStart);
}
quickDonePartition = true;
}
function quickSteps(qStart, qEnd){
quickSetPivot(qStart, qEnd); //do the first pivot, so that left = smaller and right = larger
let pivotLocation = quickSmallerIndex; //store pivot location in case changes are made
if(pivotLocation > qStart + 1){ //sort everything on the left
quickSteps(0, pivotLocation);
}
if(pivotLocation < qEnd -2){ //sort everything on the right
quickSteps(pivotLocation, qEnd);
}
quickSortEnd();
}
function quickPartition(){
//Set the new index
if(partitionStep == 1){
partitionStep++;
setClass(nodes[quickLargerIndex - 1], 2, "Index");
addStep("Compare Index Node " + quickLargerIndex + " to pivot node " + (quickRightIndex) + ".");
return;
}
//put the index in the correct pile
if(orderArray[quickLargerIndex - 1] < quickPivot){ //The Node is smaller than the pivot
numSwap(quickLargerIndex - 1, quickSmallerIndex); //swap the smaller array with the first unpartitioned node
addStep("Index Node " + quickLargerIndex + " is smaller than pivot node " + (numNodes)+ ". Put the index node at the start and set it to \"smaller\".");
setClass(nodes[quickSmallerIndex], 2, "Combined"); //set Smaller Node to the smaller color
swapArray(orderArray, quickSmallerIndex, quickLargerIndex - 1); //Sets the node the the begining of the list
quickSmallerIndex++;
}else{ //This node is larger than or equal to the pivot
addStep("Index Node " + quickLargerIndex + " is larger than pivot node " + (numNodes)+ ". Set the index node to \"larger\".");
setClass(nodes[quickLargerIndex - 1], 2, "Special"); //set larger node to the larger color
quickLargerIndex--;
}
partitionStep = 1
}
function quickSortEnd(){
addStep("The list is now sorted");
randomizeButton.disabled = false;
done = true;
clearInterval(wait);
sortSelector.disabled = false;
randomizeButton.disabled = false;
}
/*
Heapsort first makes the array into a max-heap.
Then, it switches the head with the first unsorted node. The head is now sorted.
Then, sink the new head to it's proper location
Heap = "Relevant"
Current = "Current"
Parent = "Special"
Larger Branch = "Parent";
*/
var heapStarted = false;
var makeHeapStep; //making a heap has two distinct steps, adding a Node to the heap and swiming it up
var numHeapNodes; //the number of nodes in the heap properly
var heapSize; //The Node we are about/just added to the heap
var currentHeapPosition; //The position of the current Node
var heapSwimStep; //Swimming takes two steps. Setting the parent and swapping if needed
var heapParentIndex; //Index of the parent of the current Node
var heapOldPosition; //The immediate previos position of the current Node. Helps for coloring
var doneMakingHeap; //are we done organizing the nodes into a heap?
var sortHeapStep; //Sorting a heap has 2 distinct steps, swapping the head node, and sinking the new head
var heapLargerBranch; //stores the index of the larger child
var heapSinkStep; //sinking has 2 distinct steps. Setting the larger branch, and switching
function startHeap(){
makeHeapStep = 1;
numHeapNodes = 1;
orderArray = getOrder();
console.log(orderArray);
heapSize = 0;
heapStarted = true;
heapSwimStep = 1;
heapOldPosition = -1;
doneMakingHeap = false;
sortHeapStep = 1;
heapSinkStep = 1
}
function runHeapStep(){
if(!heapStarted){
startHeap();
}
heapStep();
}
function heapSort(){
if(!heapStarted){
startHeap();
}
wait = setInterval(heapStep, delay);
}