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.

849
185
876
221
417
511
702
333
698
258
755
562
548
45
924
597
311
880
692
952
179
813
944
274
646
241
759
224
489
383
115
983
351
227
899
250
575
54
259
761
540
164
32
223
600
519
630
834
349
315
492
11
913
522
271
913
645
325
567
903
130
609
198
686
777
356
218
420
908
941
149
426
253
99
184
448
129
394
927
794
683
977
779
107
287
548
224
759
480
951
564
393
650
193
941
294
425
593
898
374
BubbleSort - 0 steps
345
338
592
751
456
818
143
560
552
659
109
953
770
955
830
676
660
400
377
648
492
257
686
581
796
502
294
564
77
54
103
697
282
7
20
350
841
903
989
32
146
90
154
717
149
486
856
36
696
78
553
417
982
793
503
271
170
857
999
569
734
298
994
643
511
469
657
290
518
299
661
524
263
291
708
427
215
970
307
609
995
513
551
712
318
90
802
892
988
826
139
817
940
463
587
48
592
92
609
558
InsertionSort - 0 steps
867
808
139
8
33
102
540
196
843
651
792
730
535
866
400
755
142
589
249
884
485
888
468
441
222
213
589
264
294
856
920
661
174
956
643
599
455
733
844
484
908
115
965
236
297
135
956
231
281
20
920
521
147
206
303
803
102
521
259
573
116
706
220
44
166
326
255
175
761
725
405
756
587
129
187
799
572
212
528
660
813
939
998
534
841
689
262
452
676
230
56
487
198
284
494
54
300
465
215
167
ShellSort - 0 steps
926
452
462
979
233
187
229
235
884
885
251
903
515
371
242
995
606
694
895
842
800
927
38
552
282
157
463
599
972
257
995
846
694
147
988
643
318
787
378
58
584
244
374
299
375
858
976
711
604
988
108
359
623
26
300
26
446
221
990
263
260
147
404
874
990
561
274
583
365
780
195
983
234
67
309
565
809
434
324
146
665
371
329
334
367
832
718
294
237
30
927
613
666
544
468
353
906
400
539
876
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