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.

427
583
736
852
635
116
333
604
975
432
688
587
268
850
674
478
31
343
211
685
492
373
920
576
434
13
254
1000
92
455
73
240
156
605
290
894
824
892
542
823
604
395
840
969
858
602
684
873
363
278
619
664
143
871
329
784
258
841
748
833
496
25
922
577
386
516
621
194
670
453
980
910
575
949
296
474
922
804
389
292
884
248
840
473
628
84
191
107
529
360
29
3
114
63
350
421
934
491
241
636
BubbleSort - 0 steps
712
962
947
144
773
830
126
499
34
691
430
861
39
88
964
952
436
586
797
796
737
56
407
160
276
89
341
237
424
357
416
268
650
276
465
826
736
401
901
715
901
453
787
367
172
652
305
59
152
552
270
283
552
961
590
908
894
949
153
137
836
800
239
267
689
410
36
151
983
554
722
871
890
188
559
855
416
74
415
964
761
508
723
958
889
880
947
531
835
183
828
374
312
155
518
814
223
911
664
145
InsertionSort - 0 steps
420
613
814
135
973
394
506
638
561
919
764
199
325
28
637
525
259
942
362
830
897
599
172
206
119
475
585
760
383
748
106
751
465
16
83
292
12
283
17
612
767
820
686
506
968
450
360
603
171
356
61
460
82
575
794
608
789
615
606
390
238
315
226
938
730
322
923
861
495
811
333
26
525
590
554
199
48
172
976
254
38
828
648
21
312
162
576
126
185
460
942
524
922
31
251
564
706
712
573
956
ShellSort - 0 steps
122
231
984
338
55
998
211
650
372
885
361
405
778
321
465
603
334
448
286
907
273
304
596
110
678
50
114
533
978
533
887
921
553
154
150
130
561
211
841
14
206
607
647
556
148
834
474
77
393
462
637
763
715
702
181
651
578
543
506
724
437
213
143
636
757
258
777
659
445
685
897
923
250
818
551
183
536
528
518
860
399
266
868
156
949
465
308
381
595
110
5
608
338
123
76
625
100
688
106
381
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