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.

862
801
333
655
891
863
835
74
188
33
284
799
697
876
809
873
313
122
158
851
428
981
795
788
54
94
822
140
313
563
611
193
943
744
681
258
948
732
524
340
561
490
9
889
251
762
719
837
261
913
659
473
139
896
514
507
352
404
232
453
269
136
709
428
569
687
4
232
393
258
248
649
55
35
542
791
5
411
359
333
830
730
324
974
777
430
293
85
395
697
192
792
235
605
569
19
222
564
341
792
BubbleSort - 0 steps
269
113
998
637
24
655
741
421
700
310
949
801
868
861
324
929
189
534
319
661
252
978
132
517
13
537
937
742
44
678
211
393
306
384
448
779
193
901
780
93
35
179
205
105
300
917
605
46
227
313
867
323
845
514
465
282
301
317
675
326
376
848
210
184
11
952
256
376
467
453
745
582
65
524
95
919
433
499
170
121
50
995
732
402
505
485
914
677
923
129
69
162
100
816
241
843
933
110
857
604
InsertionSort - 0 steps
273
266
800
528
605
12
720
905
956
703
419
413
464
851
862
434
792
639
773
312
972
793
446
182
488
829
580
388
332
444
31
992
227
725
886
217
354
942
578
312
918
308
139
259
746
348
521
227
1
833
768
622
251
311
912
491
283
448
65
951
1
907
227
848
982
850
414
859
61
386
758
857
386
399
973
373
427
459
789
494
520
289
708
730
818
258
813
38
420
687
82
647
894
195
679
902
624
116
233
213
ShellSort - 0 steps
765
565
438
615
154
514
730
434
572
431
110
5
840
654
898
686
657
294
118
286
850
734
439
730
108
850
852
410
334
601
905
322
10
144
446
392
501
987
852
852
54
929
956
1
551
764
187
894
151
838
944
347
317
703
37
32
448
333
678
853
664
168
303
885
343
314
25
582
544
169
184
584
758
73
792
973
907
525
608
953
73
527
626
122
753
508
567
848
246
592
709
178
630
239
23
899
578
276
346
223
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