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.

182
425
657
265
903
638
885
877
3
794
870
403
184
217
248
243
752
298
306
151
974
864
9
113
313
780
59
890
646
325
744
45
383
360
552
75
698
899
739
701
429
120
365
725
39
120
106
742
233
253
544
639
195
549
526
355
674
483
26
924
834
888
576
383
300
228
823
671
973
108
748
264
781
310
225
639
541
726
399
8
182
618
497
222
82
487
138
245
593
935
401
984
860
505
605
284
708
980
655
216
BubbleSort - 0 steps
21
427
27
295
173
961
509
786
156
182
249
259
293
59
744
700
681
672
509
204
787
401
252
566
72
833
768
686
713
621
766
641
34
5
615
108
632
415
259
670
53
272
882
201
560
38
45
886
920
908
817
372
955
667
195
553
984
880
552
606
423
610
146
697
865
850
724
714
397
947
484
89
691
701
218
983
382
289
725
785
159
189
143
268
697
610
465
210
126
921
459
562
115
597
948
524
330
816
388
575
InsertionSort - 0 steps
192
526
403
943
59
801
832
520
588
496
702
265
373
959
245
492
651
519
212
435
2
896
842
625
261
444
37
468
513
404
239
721
879
825
962
49
627
813
879
885
399
231
892
302
73
327
621
18
392
938
870
354
473
219
933
116
439
831
843
705
273
84
113
283
603
227
237
640
412
932
998
170
315
254
661
305
307
147
95
262
918
368
725
545
629
679
109
899
645
998
173
547
76
954
24
990
391
821
516
123
ShellSort - 0 steps
257
954
363
667
857
57
305
858
585
684
751
550
105
778
411
238
922
15
337
553
522
607
407
235
843
661
548
751
571
313
509
273
621
48
872
926
325
309
411
270
68
867
943
650
205
95
590
562
554
355
885
607
476
106
649
194
672
397
871
325
672
156
986
504
493
578
933
652
78
553
920
406
465
541
792
834
711
733
155
524
107
618
374
281
215
142
954
721
834
904
226
175
242
41
920
55
692
704
75
733
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