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.

548
404
957
70
680
525
56
470
75
149
213
745
311
804
125
429
277
340
491
509
949
792
717
701
172
809
636
321
620
233
91
7
921
932
406
497
666
816
518
732
322
167
692
438
246
636
82
988
713
42
837
851
304
548
657
41
293
929
704
120
859
874
454
973
799
36
421
865
603
74
577
402
16
184
344
507
898
183
492
706
417
943
468
92
653
848
147
874
413
127
45
347
325
337
403
564
619
595
194
542
BubbleSort - 0 steps
412
593
807
827
415
610
348
171
134
241
847
126
153
506
35
372
137
818
765
652
794
784
374
638
491
323
379
863
912
332
918
412
240
888
909
956
852
803
824
178
108
395
70
820
210
955
622
169
350
912
496
604
453
131
787
568
861
479
818
592
45
677
85
923
888
849
486
28
772
807
93
100
484
757
62
786
257
801
192
456
753
363
23
961
135
995
393
691
137
153
764
755
547
298
870
34
389
874
78
830
InsertionSort - 0 steps
982
66
267
184
155
891
473
789
59
666
224
208
392
748
928
355
620
461
517
167
885
999
379
898
965
818
395
198
262
331
474
976
787
527
495
399
933
560
27
189
580
699
623
606
1
658
426
2
812
130
428
475
122
181
250
69
924
831
687
825
948
443
453
70
833
438
131
220
615
827
47
129
108
656
996
63
329
856
181
353
955
551
303
104
56
325
365
910
946
446
112
136
218
812
422
757
861
31
600
487
ShellSort - 0 steps
199
963
461
521
295
602
126
288
74
544
707
265
848
821
700
252
240
331
495
60
844
594
711
495
38
884
237
670
65
845
608
165
333
135
701
508
86
523
3
849
28
624
100
564
269
484
110
793
866
519
198
698
216
298
459
707
211
338
918
308
601
835
440
467
803
334
543
6
818
168
931
429
682
372
625
117
272
908
93
29
203
447
270
919
550
931
525
125
512
737
635
257
925
176
489
818
676
958
580
134
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