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.

507
10
650
24
262
48
438
573
347
272
995
784
761
829
882
613
253
937
903
123
250
369
276
475
664
231
819
772
227
446
461
416
409
607
132
317
216
595
420
109
7
18
610
548
796
517
978
259
339
513
879
969
743
467
113
177
120
915
29
379
458
119
222
303
480
597
20
309
165
304
974
190
55
257
908
132
740
573
11
939
386
82
726
330
369
585
692
657
679
157
305
301
438
84
480
869
562
453
701
441
BubbleSort - 0 steps
176
14
99
551
895
928
117
543
160
461
467
798
765
904
597
131
997
167
238
578
515
338
441
571
714
515
906
737
139
402
97
217
28
132
852
262
611
76
528
749
486
367
254
119
197
575
23
718
583
714
227
698
569
858
79
910
731
116
82
217
614
465
436
576
130
524
846
664
477
275
623
231
621
918
495
513
153
624
548
270
202
570
124
884
666
316
345
165
505
332
835
624
513
805
316
50
66
499
538
453
InsertionSort - 0 steps
87
889
175
970
499
670
850
286
293
344
313
495
166
725
581
8
451
656
4
271
526
667
408
465
584
192
839
478
581
817
258
649
627
750
233
839
984
656
133
677
43
806
94
191
489
793
261
249
536
825
951
838
29
747
950
64
209
543
669
840
263
124
425
362
673
944
919
376
349
2
133
860
429
550
469
262
664
681
471
91
448
882
671
822
74
251
689
672
671
693
831
529
632
146
916
533
529
520
370
968
ShellSort - 0 steps
885
920
424
618
765
432
826
753
243
475
806
385
195
618
666
831
204
848
161
117
681
506
642
503
436
20
598
501
264
903
309
330
876
805
202
445
418
258
591
449
837
236
603
783
709
990
287
41
2
590
213
102
23
424
179
285
725
603
21
8
820
519
25
737
51
219
808
825
805
86
724
355
576
404
210
262
139
441
768
407
520
184
746
556
948
805
812
342
753
64
115
440
260
584
41
483
118
782
269
998
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