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.

413
587
462
478
405
856
309
833
897
909
680
734
911
441
262
734
876
918
806
601
826
907
280
408
233
902
136
249
479
181
153
187
945
293
931
756
471
41
33
640
868
577
582
910
963
231
46
456
895
543
141
982
51
917
118
204
813
836
591
463
258
997
62
8
556
638
299
296
591
781
223
270
923
352
363
754
323
521
408
468
649
582
38
678
857
215
806
915
899
251
458
938
881
277
182
367
534
561
286
35
BubbleSort - 0 steps
242
897
677
334
351
540
510
910
247
413
948
916
82
204
539
155
236
356
930
911
472
378
579
519
492
525
95
355
584
661
813
467
526
647
327
466
57
822
707
445
473
759
63
160
895
816
649
728
387
811
623
806
613
569
622
970
628
593
672
73
124
152
251
771
779
866
776
860
446
905
508
349
207
814
720
385
232
921
686
30
256
149
293
287
238
226
189
786
258
810
398
986
112
545
190
272
41
539
607
916
InsertionSort - 0 steps
449
850
379
109
705
931
229
355
966
359
76
525
996
426
885
364
486
779
518
770
310
811
368
8
281
357
311
315
33
757
436
710
758
992
251
217
573
300
472
646
703
654
967
675
920
858
729
933
12
356
663
823
157
778
15
238
374
883
435
30
214
828
169
673
129
454
628
908
6
930
345
41
590
77
937
870
238
736
133
913
545
787
442
687
98
416
636
651
230
427
97
6
605
856
81
849
264
597
620
926
ShellSort - 0 steps
836
597
435
744
295
39
170
116
127
190
416
49
178
227
668
429
738
258
358
544
571
122
27
487
651
863
494
761
334
979
273
355
694
976
829
921
650
911
705
755
665
639
342
944
165
384
868
264
964
684
528
582
400
370
199
56
406
187
710
175
923
361
605
621
474
75
307
861
235
640
191
337
572
524
234
559
744
817
66
559
534
146
496
107
304
739
179
783
931
925
731
619
704
722
962
925
787
742
793
512
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