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.

580
935
469
518
187
291
746
542
588
646
782
203
601
151
132
84
665
667
255
732
830
31
606
45
107
739
572
295
677
923
958
881
764
938
849
510
724
750
988
997
209
922
613
613
991
8
143
673
300
847
75
540
663
984
263
700
265
468
379
356
398
739
702
258
801
55
161
582
918
851
622
400
6
566
133
86
739
169
923
185
939
78
274
150
438
579
715
124
314
713
992
403
5
613
926
752
279
659
451
787
BubbleSort - 0 steps
489
572
493
959
237
339
797
236
62
773
276
617
598
709
397
150
378
630
552
773
157
317
431
361
835
62
475
410
865
809
316
122
180
686
57
633
677
327
960
10
747
818
698
739
828
20
543
799
312
589
961
27
761
869
116
258
412
120
668
693
559
670
865
35
449
194
265
711
380
186
981
504
681
120
793
805
809
937
293
867
657
9
164
356
417
714
561
378
224
932
954
783
116
316
738
880
105
124
265
419
InsertionSort - 0 steps
429
382
545
740
963
569
778
479
778
904
919
442
894
626
435
982
463
258
516
269
512
647
447
629
818
264
234
99
759
184
120
388
629
949
581
975
28
910
576
191
308
159
517
307
887
270
734
319
148
665
724
466
584
304
460
301
132
22
927
904
329
305
20
1000
186
605
928
669
548
204
383
765
872
194
112
64
185
880
525
367
873
44
801
188
959
239
250
926
647
135
720
367
702
296
994
48
247
898
10
301
ShellSort - 0 steps
632
122
175
545
834
815
617
505
645
168
491
828
668
885
545
16
206
376
564
575
734
671
887
181
639
611
803
362
859
992
989
10
24
150
91
157
863
765
673
100
406
374
605
753
361
30
171
499
986
608
293
934
48
731
619
792
458
275
407
716
46
939
711
144
829
774
960
227
981
333
384
900
22
983
319
762
865
244
706
475
517
197
330
230
862
531
264
796
735
278
567
418
956
273
109
990
571
418
714
349
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