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.

553
337
519
245
88
919
447
632
399
900
969
957
545
465
125
4
893
205
810
969
85
65
409
840
310
571
839
363
67
960
428
12
631
228
575
185
240
110
898
311
103
929
639
89
22
324
218
162
391
357
75
727
754
970
194
306
983
31
798
230
142
112
74
84
318
334
653
471
39
442
985
878
976
482
97
115
574
961
375
277
500
460
929
978
200
359
319
965
698
224
540
879
402
842
401
433
289
484
719
749
BubbleSort - 0 steps
819
983
788
636
877
273
943
225
926
11
536
875
194
764
482
318
585
242
239
936
633
426
811
974
453
126
679
284
452
638
65
758
366
807
411
353
963
83
99
793
492
980
210
668
238
952
845
775
553
929
983
264
13
877
50
325
720
470
593
993
972
176
577
750
894
634
684
606
137
410
542
689
219
123
297
267
989
613
352
514
49
879
308
855
378
81
695
609
611
786
808
11
797
181
81
830
717
699
203
603
InsertionSort - 0 steps
953
326
456
594
685
933
109
378
36
677
483
673
782
753
638
786
738
779
56
847
996
878
646
287
510
706
646
169
222
83
135
342
813
107
444
360
625
980
995
589
552
887
818
657
538
819
542
251
581
771
925
647
865
508
543
802
232
338
477
420
702
700
370
439
67
924
870
629
733
595
801
374
319
910
47
261
929
439
927
1
361
703
189
153
593
294
956
837
50
573
388
355
337
177
570
744
886
758
58
875
ShellSort - 0 steps
652
522
752
748
787
471
493
328
124
137
609
681
658
867
599
166
456
730
257
880
625
199
713
180
610
713
199
752
761
978
419
336
642
176
233
164
405
483
851
399
889
943
37
379
413
452
572
563
725
348
694
141
712
681
711
177
404
820
782
738
317
763
306
858
83
323
448
185
308
171
984
703
729
84
941
65
894
833
287
152
822
705
774
737
646
2
642
617
557
511
980
841
623
765
496
880
409
971
645
688
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