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.

958
20
148
59
749
596
415
794
446
641
634
902
626
532
456
378
180
346
258
217
123
497
58
264
577
983
713
999
548
278
160
420
199
43
977
562
642
745
797
938
368
641
730
808
718
294
411
423
238
404
782
907
260
191
433
838
49
821
481
573
359
582
874
605
793
549
62
701
183
867
491
365
13
870
5
105
442
11
790
512
192
319
237
814
795
36
634
460
109
78
356
930
545
712
798
100
268
120
517
871
BubbleSort - 0 steps
672
379
948
536
973
163
348
127
994
498
286
472
170
202
700
718
824
488
107
703
175
293
30
367
484
154
833
693
388
402
207
20
553
194
563
3
268
943
987
836
538
523
462
863
100
696
177
953
55
500
779
407
931
806
384
940
935
535
453
452
898
922
452
459
905
969
268
898
40
919
27
403
616
387
207
166
14
933
843
178
211
507
817
301
5
650
971
598
617
743
732
677
801
713
705
127
664
240
202
329
InsertionSort - 0 steps
686
894
696
896
996
646
183
592
575
294
805
1000
241
710
186
396
827
215
753
772
265
174
761
825
516
138
468
313
716
428
97
557
997
415
922
677
98
407
577
708
254
897
349
237
860
256
341
2
337
129
339
840
476
393
472
212
401
650
324
905
882
629
752
849
202
974
568
977
507
563
853
207
96
814
645
157
100
651
183
916
858
455
401
194
801
600
991
856
910
313
273
864
80
716
483
58
751
719
794
780
ShellSort - 0 steps
308
619
748
987
9
476
940
502
74
97
187
636
974
832
573
123
570
417
725
134
146
827
89
24
571
170
947
675
30
754
746
981
86
326
953
83
898
740
561
229
207
796
850
113
106
609
135
518
529
231
720
505
114
695
736
352
772
197
101
136
466
120
109
60
161
721
166
528
987
474
813
738
276
495
652
190
723
276
423
129
240
146
554
846
832
753
182
722
592
623
828
420
323
281
939
315
288
234
41
680
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