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.

338
138
888
729
139
769
510
528
883
379
308
584
255
777
793
93
154
495
772
168
740
121
590
774
699
908
803
777
298
390
55
770
141
248
54
418
308
739
442
212
899
853
47
303
331
747
315
955
280
594
518
470
811
575
478
261
276
718
1
103
597
895
75
50
781
45
79
159
465
724
40
818
4
309
63
513
578
984
588
388
39
62
444
26
174
768
188
50
265
888
174
630
679
932
700
775
666
312
912
507
BubbleSort - 0 steps
921
978
412
775
755
12
165
655
953
884
707
599
997
956
653
544
563
856
905
508
891
327
898
399
627
892
270
859
161
902
473
430
513
750
564
261
156
308
844
115
305
46
951
617
787
263
923
1
183
179
160
153
649
166
664
672
21
754
707
355
939
917
266
207
388
245
495
617
922
20
874
2
989
391
731
247
937
354
552
283
324
289
871
571
888
57
27
13
508
802
773
974
30
21
748
777
180
704
288
372
InsertionSort - 0 steps
366
683
307
313
219
149
282
566
37
423
300
217
793
214
766
23
221
380
763
916
543
509
525
582
736
948
771
151
580
165
636
106
455
987
545
38
70
561
935
371
445
135
279
654
388
657
520
561
667
511
192
198
586
738
147
301
291
360
980
734
704
588
723
731
402
343
966
34
160
968
819
757
396
417
484
737
363
79
196
41
759
897
824
274
758
219
124
442
747
252
755
658
220
303
970
455
423
759
381
610
ShellSort - 0 steps
870
815
488
817
367
406
404
115
225
772
535
872
396
593
300
69
539
694
942
982
702
382
307
906
450
125
144
483
506
337
207
765
526
637
952
401
839
871
824
308
968
380
972
631
513
788
806
251
573
450
452
863
380
394
111
193
613
268
908
401
895
200
870
434
960
738
386
157
942
873
131
888
475
851
294
722
774
290
832
800
879
225
592
52
267
879
69
923
245
513
456
47
547
498
541
496
861
684
302
16
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