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.

125
940
237
930
835
133
105
493
939
453
482
942
577
299
339
23
975
345
294
631
879
158
574
707
270
518
487
709
162
298
980
992
909
54
371
719
401
888
701
783
979
700
169
817
117
321
817
417
915
944
324
749
906
529
820
468
171
387
643
338
479
815
542
750
903
578
303
113
74
283
130
122
912
276
312
832
471
215
997
857
761
708
374
69
778
807
915
560
443
205
377
376
212
950
382
485
739
829
451
511
BubbleSort - 0 steps
487
108
641
910
535
88
793
955
957
379
334
679
773
628
877
40
80
691
988
634
437
630
470
974
852
848
377
765
653
758
437
249
479
611
585
115
227
145
506
814
913
791
431
859
730
910
202
235
973
705
112
381
806
477
222
50
990
577
452
452
790
116
103
745
133
623
763
732
898
782
281
703
213
640
826
324
564
262
550
379
964
948
361
762
248
999
408
488
300
943
66
864
336
48
569
684
439
96
823
95
InsertionSort - 0 steps
257
67
752
341
683
262
226
54
748
76
399
715
430
461
755
972
546
462
551
409
454
544
548
146
674
370
947
526
980
391
583
241
704
159
601
339
538
242
671
1
827
30
761
604
113
291
477
179
884
79
453
82
956
420
318
725
441
942
284
719
931
618
634
639
158
575
162
896
729
476
293
931
146
727
315
226
809
786
681
333
189
339
817
607
436
988
96
497
9
939
6
836
879
121
946
893
842
131
229
656
ShellSort - 0 steps
843
913
176
792
131
687
604
894
861
829
910
998
737
693
940
220
251
690
712
512
764
190
39
464
750
94
488
492
457
962
759
540
539
857
852
366
768
1000
824
807
447
475
900
346
429
147
109
486
101
733
807
92
881
800
246
812
801
302
71
861
446
242
991
743
758
488
8
313
904
435
410
997
495
188
418
52
49
871
589
373
87
374
705
958
519
433
988
821
451
446
99
299
259
419
69
663
415
597
538
110
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