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.

855
443
517
728
208
133
477
1
321
103
956
710
422
570
267
399
576
594
123
62
574
29
156
580
111
292
588
953
681
806
280
923
402
207
682
651
148
86
394
238
836
417
350
733
409
977
718
199
899
845
376
26
501
499
128
584
338
485
878
719
848
142
899
132
436
562
82
741
583
247
101
89
840
423
792
346
32
955
326
207
997
383
708
455
364
416
559
370
400
707
202
650
625
264
268
41
765
423
217
675
BubbleSort - 0 steps
416
165
755
28
471
822
489
746
439
380
584
753
742
438
227
265
754
30
209
650
557
582
821
30
177
792
159
467
341
616
122
716
634
479
696
80
57
421
158
579
290
480
736
820
474
480
705
22
698
92
970
384
221
168
787
714
581
270
546
273
298
51
270
341
856
983
780
57
773
310
641
609
302
71
621
57
718
651
970
534
654
729
274
487
501
214
87
471
948
311
623
438
957
869
618
110
23
807
528
141
InsertionSort - 0 steps
467
541
717
891
711
320
398
693
697
665
938
63
372
874
658
168
214
638
575
10
903
695
615
353
659
185
856
226
577
980
204
520
519
470
178
795
672
146
255
667
546
17
316
806
344
54
905
230
471
744
958
825
974
167
488
829
528
272
935
76
254
25
827
24
43
818
471
458
685
64
225
593
346
659
273
867
47
854
483
265
194
749
219
971
919
196
290
930
593
72
900
900
330
794
461
900
537
584
953
880
ShellSort - 0 steps
962
529
308
282
549
576
839
907
32
398
206
970
160
837
562
120
984
564
207
243
584
53
972
460
858
938
337
928
818
390
352
944
36
927
723
813
613
124
647
687
396
696
432
569
16
900
386
938
357
627
333
460
759
224
765
625
925
156
64
919
949
876
513
443
373
879
334
329
145
446
227
781
355
791
166
606
371
455
51
657
153
402
334
467
61
902
988
791
235
307
155
375
80
860
393
34
443
841
676
911
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