skip to content

JavaScript: Sorting Algorithm Comparison

In this article we present a visualizaion of four different JavaScript DHTML sorting classes all of which have been described in more detail in previous articles.

Sorting Algorithm Visualization

Below you will see four scrambled versions of the same image. When you use the controls below to 'solve' the puzzles they will each use a different sorting algorithm as indicated - Bubble, Insertion, Shell and Quick Sort - to rearrange the pieces.

You can watch in real time as the sorting takes place and see an updating counter of the number of steps taken so far - where a 'step' is the process of exchanging two puzzle pieces.

931
871
647
630
793
435
924
622
731
634
450
369
207
445
276
54
68
114
582
407
382
143
37
497
179
133
614
920
743
276
72
317
805
734
891
49
441
14
138
422
118
825
397
285
76
345
855
280
214
317
354
697
396
93
745
449
537
420
442
984
261
688
12
586
607
327
187
958
649
340
109
739
871
744
667
45
784
273
449
676
919
967
51
812
76
601
312
567
28
517
880
58
191
776
177
492
394
495
618
567
BubbleSort - 0 steps
131
810
573
496
342
335
259
268
478
911
631
277
766
508
499
31
445
86
132
772
893
504
620
787
428
777
113
400
532
661
366
406
492
585
956
13
742
285
399
87
392
32
292
840
821
54
315
772
825
882
453
28
824
160
408
942
42
580
665
972
932
131
933
171
589
282
414
433
862
624
921
468
911
202
159
783
231
838
752
774
628
798
540
298
385
870
326
748
61
678
55
511
963
521
41
824
707
891
539
552
InsertionSort - 0 steps
835
701
665
422
492
723
453
503
949
168
57
523
434
612
65
613
910
839
122
458
918
846
317
695
970
354
835
285
684
794
885
441
866
771
292
623
437
850
214
872
5
857
963
351
604
330
713
830
823
914
254
775
511
147
862
738
780
138
185
752
512
482
606
787
93
594
307
234
593
941
24
854
930
284
843
236
835
686
25
474
463
509
673
421
456
534
489
258
226
331
563
107
51
654
341
15
394
840
139
64
ShellSort - 0 steps
539
722
191
6
115
155
720
31
317
635
139
485
514
227
135
124
511
628
507
936
345
244
811
508
202
32
921
654
412
889
865
804
172
704
788
431
329
266
465
850
738
217
228
764
584
474
623
254
860
805
344
382
123
778
483
777
238
817
280
328
167
521
641
221
306
200
247
784
487
164
811
648
249
155
995
355
422
138
179
521
167
777
889
633
292
819
642
766
395
675
691
643
326
855
26
115
598
867
15
743
QuickSort - 0 steps
Controls 1) Select an image; 2) Click 'SOLVE'. * images generated by Stable Diffusion and Midjourney

All of the sorting is powered by JavaScript in your web browser so there is no load at all on the web server. There is also only a single background image being used each time - they haven't been sliced up into smaller squares for the puzzle.

While there are other methods for shuffling and sorting values, the advantage of DHTML sorting - rearranging actual HTML elements within the DOM - is that it preserves any event handlers or other dynamically assigned properties that may have been assigned to the elements.

This is possible because we are working with a 'live' NodeList which means that "changes in the DOM automatically update the collection."

Comparison of Results

As expected, the Bubble Sort and Insertion Sort algorithms are relatively slow requiring a large number of steps to solve the puzzle. This is mainly down to the fact that they can only swap adjacent squares.

The Insertion Sort and Quick Sort algorithms are significantly faster thanks to their more advanced algorithms requiring only a fraction of the number of steps each time to reconfigure the puzzle pieces.

We generally use the Shell Sort algorithm which, despite being slightly slower, is a stable sort, whereas Quick Sort is unstable (a sorting algorithm is said to be stable "when two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array").

What do we use if for?

Apart from these fascinating visualizations we typically use JavaScript DHTML sorting when presenting tabular data. It allows us to have the table contents sorted by various values on demand without needing to re-request data from the web server.

You can see some examples of this in earlier articles on the subject. The code used here for the visualization has been adapted slightly to insert a delay, but is otherwise identical to the code presented there.

We were able to insert delays into the sorting process by converting the exchange step to use a generator function which is then called repeatedly by setInterval. Generators have the effect of allowing you to 'pause' and 'resume' execution within a function.

Another interesting use case would be maintaining a 'pole position' graphic where race data was being dynamically inserted into the page and the task was to keep the list in the right order - perhaps with a touch of animation.

If you find a use for this code in your website or project please let us know using the comments button below.

< JavaScript

Post your comment or question
top