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.

220
845
331
757
929
608
825
92
459
584
447
72
518
395
567
473
1
305
706
8
59
191
190
814
757
174
52
754
672
784
323
275
993
365
176
920
661
282
639
362
239
614
828
528
147
21
388
136
394
872
605
289
520
154
397
847
771
658
375
698
665
542
90
648
121
497
176
706
337
502
670
979
919
103
227
521
958
419
561
397
688
878
916
911
948
476
996
899
202
852
188
592
438
95
535
859
303
553
928
826
BubbleSort - 0 steps
96
786
532
795
78
188
373
644
870
569
752
601
294
951
245
622
82
94
523
690
710
872
768
550
403
796
404
310
274
581
616
659
747
276
742
897
417
953
69
805
561
551
891
845
713
479
131
695
793
714
569
680
448
346
980
463
101
176
415
113
271
488
221
222
915
812
806
963
117
972
745
65
440
495
19
748
409
482
378
176
953
195
173
873
209
706
711
485
637
14
249
336
998
162
60
88
793
325
500
677
InsertionSort - 0 steps
970
484
700
897
283
241
401
42
695
99
410
610
371
252
239
776
377
466
681
929
938
728
447
291
877
766
896
720
278
664
822
782
438
718
715
28
592
287
776
732
414
243
316
420
816
372
468
260
518
309
101
854
668
519
334
972
71
979
1000
246
463
867
120
159
818
204
937
196
941
150
614
792
941
503
119
606
158
511
948
881
288
578
112
797
972
454
723
171
214
97
858
906
935
830
433
922
336
543
330
228
ShellSort - 0 steps
153
688
941
899
251
884
538
403
267
892
241
604
995
261
374
160
318
40
543
809
38
707
563
240
214
649
582
554
511
150
699
887
980
778
491
881
339
108
344
84
258
805
460
910
555
493
998
486
270
704
497
942
221
454
217
759
149
1000
275
701
961
607
174
270
160
722
367
309
450
820
241
674
538
291
534
685
347
582
598
822
744
508
638
101
421
303
156
406
639
803
911
997
618
85
981
941
316
858
867
816
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