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.

415
380
149
433
27
174
734
576
85
821
147
533
471
371
932
962
584
259
833
147
478
455
591
419
368
831
347
19
529
235
397
948
427
783
232
911
63
989
265
618
474
637
617
481
175
621
635
978
207
725
569
918
884
315
347
596
465
73
350
662
560
284
241
749
561
821
388
587
851
318
964
280
507
723
382
707
723
15
648
109
375
817
7
144
849
461
368
795
63
117
384
85
290
792
446
757
241
643
946
344
BubbleSort - 0 steps
111
330
528
679
950
38
41
202
700
741
803
683
528
685
344
787
552
276
350
758
71
39
562
752
85
166
889
928
639
227
388
605
861
803
886
619
41
587
895
750
567
451
95
369
455
154
313
73
579
524
339
269
811
326
125
126
525
594
646
713
793
701
519
226
392
838
331
820
265
927
779
158
761
975
458
289
720
789
474
356
581
575
535
981
981
427
611
231
854
937
725
181
88
19
345
220
836
231
670
597
InsertionSort - 0 steps
527
643
683
655
618
314
232
897
159
817
16
530
873
98
469
710
89
5
253
624
411
84
810
59
409
745
153
82
671
77
430
117
942
363
848
365
500
999
470
577
550
230
849
206
465
654
797
834
943
17
927
719
491
850
62
605
763
236
930
35
577
468
423
317
877
713
687
441
80
530
839
124
188
821
878
295
290
596
864
650
622
940
605
28
762
688
732
479
869
873
630
464
354
39
252
360
701
34
500
184
ShellSort - 0 steps
638
722
653
548
981
432
221
73
367
541
606
474
657
165
628
170
391
653
489
663
647
308
178
84
562
729
899
12
324
347
971
415
930
59
637
351
196
261
37
295
855
684
832
465
191
782
627
857
19
235
276
664
767
679
597
884
572
942
152
353
149
607
242
782
543
236
395
908
299
857
254
786
560
352
686
82
124
959
851
750
743
908
252
938
167
637
26
739
196
900
376
693
829
856
847
470
779
792
334
860
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