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.

275
894
1
361
676
15
42
882
569
908
869
262
389
577
402
548
366
987
315
6
656
445
287
426
440
758
865
491
353
822
217
103
802
403
446
523
264
509
67
978
247
271
902
219
590
299
992
83
131
846
256
160
664
746
918
468
560
846
228
50
896
569
767
371
503
482
89
51
842
663
118
841
385
237
918
434
557
362
155
886
918
585
98
834
702
474
677
314
132
847
931
521
293
333
864
50
330
489
434
165
BubbleSort - 0 steps
350
370
110
879
874
186
747
818
40
177
120
174
656
200
241
800
455
275
959
539
255
301
899
943
76
934
848
844
420
570
652
441
587
401
305
120
264
836
731
186
104
421
650
567
340
851
424
396
337
411
955
734
363
567
445
215
969
36
336
661
297
781
749
821
728
110
169
196
149
434
574
768
63
213
551
289
842
540
995
511
800
625
77
35
33
892
661
698
933
160
623
473
285
198
686
399
950
316
578
446
InsertionSort - 0 steps
268
825
215
467
959
940
584
496
269
643
629
126
489
509
165
14
262
398
209
348
402
269
882
702
257
141
455
336
346
462
880
702
237
299
569
416
147
853
948
448
445
697
417
83
885
845
506
653
507
657
557
147
226
706
641
37
151
434
326
275
374
898
264
830
953
645
1000
292
169
200
654
374
880
187
770
33
402
932
309
470
206
588
531
721
151
214
298
602
999
471
885
160
427
915
666
880
826
403
573
105
ShellSort - 0 steps
439
24
107
334
790
402
757
50
847
978
622
589
610
569
1
229
296
181
418
327
394
279
427
605
977
154
551
541
851
936
924
830
505
30
819
645
365
673
94
74
916
174
267
809
633
378
301
897
96
139
969
903
321
547
476
952
336
633
215
63
199
427
458
908
708
852
25
343
768
604
910
474
803
421
442
518
905
644
675
759
473
664
361
944
764
41
782
617
292
235
577
560
357
939
301
207
541
990
687
207
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