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.

540
153
227
781
498
812
825
673
640
369
678
684
356
628
392
753
961
301
748
308
662
273
257
450
831
800
309
684
874
52
584
137
824
814
40
450
681
492
212
899
284
172
911
60
632
911
356
228
599
208
82
8
281
217
113
645
862
699
804
674
378
610
445
309
948
147
691
732
804
202
319
535
102
567
744
712
18
246
168
51
163
586
122
424
598
34
633
749
795
725
989
121
767
261
930
430
321
647
437
968
BubbleSort - 0 steps
551
613
669
551
765
785
722
132
543
518
448
72
965
3
245
396
813
600
890
271
591
43
900
936
130
413
244
137
819
63
193
829
764
599
25
136
352
349
220
759
253
857
538
927
926
33
587
21
216
542
16
284
945
619
507
892
356
935
772
512
590
336
58
891
500
133
294
948
85
344
550
430
547
124
630
542
991
520
110
162
44
401
79
500
445
366
495
258
548
601
990
653
349
181
784
510
654
137
626
857
InsertionSort - 0 steps
376
593
58
65
904
320
926
567
176
687
717
264
729
711
743
999
102
163
379
279
82
341
780
300
448
204
984
369
14
415
836
858
617
362
72
561
434
597
151
7
280
23
128
642
537
143
412
296
375
167
495
58
478
223
161
588
102
944
317
467
598
724
761
450
74
292
318
841
346
806
293
557
868
584
575
90
403
289
161
391
658
909
304
521
614
890
339
460
579
48
185
158
547
387
311
740
178
732
807
892
ShellSort - 0 steps
777
802
803
724
328
35
504
559
599
881
184
332
822
438
251
668
138
568
223
99
727
653
314
748
918
310
977
257
801
970
246
674
974
652
304
413
661
630
318
21
373
258
93
685
921
179
615
847
240
223
776
243
235
949
25
634
243
307
924
528
125
203
55
898
192
158
159
725
533
190
49
548
169
245
882
350
680
943
852
903
865
340
263
224
490
459
345
976
248
513
577
460
151
251
753
848
643
728
134
76
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