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.

794
942
620
574
370
46
242
512
745
685
972
884
407
592
121
712
584
145
719
235
937
55
412
655
427
512
947
66
744
719
487
849
924
771
847
224
286
865
268
236
914
238
669
153
866
632
359
552
172
670
502
583
78
534
631
705
494
217
977
408
598
725
418
601
140
676
873
841
183
789
166
886
399
136
276
908
325
609
36
935
113
36
257
268
199
503
347
653
379
949
660
411
313
540
722
745
888
812
37
458
BubbleSort - 0 steps
930
908
326
550
611
379
608
473
853
830
172
767
671
846
487
766
489
166
645
250
824
192
419
93
215
746
394
101
137
161
768
959
569
826
892
758
176
319
26
491
779
39
861
38
555
369
305
93
508
707
350
636
276
431
23
709
140
656
242
229
908
520
790
919
800
851
205
530
242
452
22
490
45
111
15
848
257
648
977
406
510
527
14
660
340
451
606
380
81
44
325
529
60
458
982
56
948
58
8
954
InsertionSort - 0 steps
745
830
491
835
948
954
981
728
69
950
310
256
324
309
688
903
439
751
182
531
171
434
597
266
841
850
467
265
273
737
165
92
614
933
561
148
936
545
503
386
997
762
703
399
959
339
125
584
368
418
964
468
915
884
401
185
650
53
18
233
753
710
83
710
328
130
72
79
881
681
295
330
88
506
261
527
80
72
457
942
984
141
264
625
565
89
870
261
204
272
751
699
13
243
205
251
608
436
93
122
ShellSort - 0 steps
583
581
646
80
178
435
349
897
307
796
589
62
532
596
874
707
375
24
19
487
71
650
505
293
935
863
564
402
723
605
60
572
645
147
845
394
565
105
718
50
849
650
765
464
909
17
455
465
869
394
524
789
355
836
99
932
28
149
392
631
404
613
989
751
719
4
173
821
300
602
963
813
1
318
898
745
999
879
719
484
844
653
838
265
631
182
574
134
181
953
274
298
804
397
66
814
416
139
694
915
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