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.

961
773
131
94
295
42
479
191
766
835
451
624
206
588
225
57
619
122
532
38
158
829
667
131
479
917
156
307
227
449
985
582
776
772
558
639
429
780
429
20
661
15
837
475
612
592
147
714
148
169
818
695
653
269
374
360
765
707
539
576
180
124
991
747
944
933
95
650
781
353
556
465
92
706
318
770
20
312
434
76
52
962
466
988
30
865
334
886
862
816
48
269
250
844
663
55
678
956
449
433
BubbleSort - 0 steps
74
58
27
230
273
508
206
65
62
941
986
447
660
524
711
23
982
258
401
513
58
98
61
481
330
633
832
2
970
325
149
630
273
202
753
733
152
46
74
171
222
274
694
505
214
303
91
396
464
148
301
124
830
784
667
489
350
245
550
774
626
137
954
152
260
14
223
225
903
849
804
844
150
696
230
542
497
312
742
697
800
44
191
610
536
666
282
661
304
522
205
286
105
990
377
532
864
166
732
525
InsertionSort - 0 steps
245
698
952
946
878
446
151
213
57
225
805
719
218
736
367
83
892
677
380
397
343
461
187
521
972
510
421
95
161
465
962
717
778
142
190
632
310
136
155
790
907
156
935
217
222
878
940
870
641
114
45
515
669
216
742
776
808
293
518
997
880
631
202
613
82
415
789
98
38
999
400
262
446
351
235
676
233
331
644
65
529
12
977
963
798
434
994
61
18
781
435
406
319
341
319
226
52
68
777
559
ShellSort - 0 steps
37
137
173
24
929
664
586
631
690
746
567
621
576
564
14
192
497
242
438
241
226
586
209
52
115
116
293
730
342
537
644
194
214
567
737
949
168
933
266
866
951
389
231
38
220
579
75
241
488
482
87
386
462
267
104
733
606
702
336
847
504
872
741
176
322
35
533
300
521
492
207
751
630
732
557
447
42
306
673
265
720
304
701
402
518
828
209
686
429
521
227
79
605
574
411
392
56
552
659
852
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