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.

558
350
251
358
852
181
596
185
74
601
429
194
738
44
552
712
111
112
870
964
18
159
752
334
782
498
723
295
95
617
571
944
918
515
687
745
674
730
571
163
127
431
514
808
4
286
912
124
998
803
633
828
120
271
242
98
256
617
548
974
306
471
143
818
379
270
455
803
412
701
711
572
588
930
32
959
866
173
716
201
531
696
786
796
511
254
930
843
471
170
626
533
609
605
97
925
394
448
605
607
BubbleSort - 0 steps
257
194
264
203
568
207
603
507
132
254
602
689
640
224
212
453
801
908
607
756
486
571
593
41
267
931
604
629
996
597
261
985
335
628
616
362
162
152
402
392
341
594
636
752
231
986
705
51
13
177
231
140
294
601
752
569
46
420
811
265
845
772
974
650
975
411
37
258
12
831
506
640
281
644
92
93
665
804
744
637
342
327
9
683
199
404
550
58
978
501
562
483
301
1
463
134
617
891
367
88
InsertionSort - 0 steps
830
130
260
584
723
4
291
991
468
900
211
468
662
412
530
729
505
690
956
478
122
572
266
556
437
385
322
68
447
9
980
794
52
859
511
852
524
74
439
835
838
22
258
143
769
811
384
688
686
627
997
280
556
885
426
587
968
123
713
886
544
46
851
714
123
533
806
244
241
44
28
853
983
309
992
869
345
198
855
43
192
593
468
63
158
476
800
161
522
338
48
767
164
862
298
420
690
632
819
168
ShellSort - 0 steps
661
781
132
920
31
546
692
385
24
319
866
269
149
892
587
584
407
598
588
653
1
340
800
123
601
579
990
70
88
744
108
30
272
544
145
882
16
524
402
457
886
162
289
59
655
748
619
642
389
899
823
115
358
993
596
403
878
321
336
998
701
815
81
968
149
932
271
285
824
432
601
141
752
893
82
306
168
841
176
277
676
267
404
520
703
650
365
527
810
993
111
827
342
519
662
572
37
263
387
357
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