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.

780
411
463
319
88
442
513
829
96
951
688
577
35
301
314
631
216
937
626
162
82
597
4
54
645
921
266
640
564
52
638
359
539
68
950
820
722
533
647
625
449
867
603
302
656
271
827
959
639
769
466
719
245
747
380
123
662
958
883
58
448
270
520
162
494
339
771
852
155
224
47
239
191
888
904
131
358
903
847
255
430
491
834
755
572
371
225
552
969
16
423
186
887
749
414
518
505
655
99
361
BubbleSort - 0 steps
857
426
835
979
5
468
6
221
596
298
933
431
766
127
667
218
458
928
748
423
290
402
998
398
932
948
813
206
199
343
390
8
218
853
109
754
257
250
116
835
512
842
379
437
368
301
708
109
447
170
754
415
858
923
30
63
52
905
710
958
52
477
432
256
739
912
800
795
441
204
344
316
813
276
488
188
807
277
423
232
39
384
641
631
351
342
473
826
228
997
438
260
27
73
679
50
336
984
465
362
InsertionSort - 0 steps
935
682
413
254
396
439
984
929
997
485
539
401
830
895
91
287
940
921
505
478
142
819
407
493
554
746
865
771
963
742
254
67
877
775
295
939
706
80
562
226
721
640
282
71
401
179
484
23
323
229
753
971
995
653
402
96
616
220
128
326
391
801
663
960
31
138
285
421
515
162
969
242
150
304
787
609
896
87
753
54
850
929
859
11
352
684
343
762
218
480
597
75
322
600
580
211
388
697
179
620
ShellSort - 0 steps
474
690
280
133
322
419
741
347
639
72
242
782
263
748
171
389
222
156
580
951
971
855
74
89
16
74
121
11
231
82
779
452
225
586
359
850
822
114
693
232
922
90
84
413
841
852
507
566
238
263
464
835
680
131
289
791
840
278
386
518
823
885
183
901
939
148
220
224
595
749
897
690
462
708
652
969
196
690
400
310
741
194
141
276
617
44
48
501
77
363
17
306
675
916
411
276
755
791
680
901
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