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.

522
670
376
486
342
623
395
425
103
380
409
186
149
655
545
651
731
547
413
817
775
770
992
878
264
415
937
948
90
381
471
741
493
546
535
904
104
383
470
577
578
188
209
407
565
338
321
809
812
825
289
30
16
648
308
269
763
819
495
129
278
100
818
728
488
269
120
784
473
357
493
794
76
152
672
110
77
364
36
896
953
903
691
962
192
755
527
731
59
104
713
81
953
855
763
728
870
73
621
413
BubbleSort - 0 steps
719
452
278
496
404
128
185
440
376
676
295
396
773
51
104
3
911
890
522
440
127
110
849
939
460
549
555
670
981
897
592
156
30
842
112
995
251
484
787
331
857
282
560
392
37
180
353
560
792
108
246
379
982
833
965
477
515
420
389
521
687
223
100
817
357
651
851
628
223
457
664
533
400
141
231
284
948
88
156
194
311
666
944
200
87
72
614
74
396
455
153
643
976
350
384
485
20
713
602
863
InsertionSort - 0 steps
503
361
463
236
586
494
511
669
789
52
271
853
639
938
727
685
795
608
354
153
743
904
104
368
245
951
191
255
251
375
728
765
703
873
811
900
247
894
391
919
820
479
982
164
961
513
726
920
894
587
459
431
412
558
472
973
621
936
208
238
467
876
711
124
610
642
433
351
282
937
777
365
989
721
440
615
115
639
906
485
779
536
299
811
922
721
226
718
779
935
894
324
288
62
80
832
857
288
217
859
ShellSort - 0 steps
430
687
220
73
456
887
653
655
782
307
243
506
427
110
520
288
51
373
335
740
930
609
16
474
785
472
56
898
573
114
169
162
552
453
498
834
789
243
717
284
683
747
496
559
538
474
742
634
778
634
52
498
414
450
706
870
617
484
297
97
508
342
753
594
290
687
813
181
325
543
282
366
976
108
744
986
340
878
21
245
326
977
443
618
440
443
363
802
431
808
933
887
668
915
143
566
187
762
912
331
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