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.

226
867
813
95
66
632
460
469
635
374
852
231
686
945
30
301
421
147
91
411
818
583
468
82
934
589
879
519
292
110
901
49
578
601
272
483
939
201
81
286
684
183
374
621
145
631
576
371
656
434
228
211
148
163
896
584
736
635
419
68
825
159
302
661
340
494
116
229
434
150
838
926
741
30
878
606
58
992
193
71
80
328
290
261
361
273
505
344
376
625
210
265
2
621
182
363
346
671
205
700
BubbleSort - 0 steps
387
359
145
809
965
645
481
791
794
241
591
21
315
244
21
463
427
714
999
749
480
747
380
102
200
534
330
390
534
920
625
175
441
652
553
699
944
827
457
979
645
560
229
196
961
396
86
547
602
128
752
579
241
733
617
601
574
506
575
344
972
876
403
282
842
32
98
113
345
90
723
416
699
509
394
693
487
834
381
489
501
970
343
697
969
127
68
937
516
864
556
171
468
219
103
101
283
598
414
885
InsertionSort - 0 steps
112
363
484
780
357
367
148
368
320
527
229
398
653
259
947
89
97
97
750
363
82
965
324
758
580
646
349
929
247
402
882
60
792
888
672
526
262
1000
17
582
549
155
211
760
761
624
480
74
1000
947
310
671
967
290
913
69
482
55
1000
781
476
548
141
617
663
643
290
190
851
795
236
566
304
43
179
913
985
229
689
904
546
437
891
56
38
529
418
989
401
503
674
793
626
607
323
822
954
546
534
496
ShellSort - 0 steps
600
232
57
425
678
484
326
876
949
497
559
338
650
497
275
491
264
332
458
929
480
345
196
696
246
369
601
603
767
402
866
46
665
409
540
658
359
782
620
257
944
455
687
214
787
944
726
885
370
35
59
214
945
98
523
347
500
476
362
755
93
860
695
654
404
78
229
474
383
761
269
6
588
615
963
704
259
740
701
793
71
90
988
410
339
612
462
168
302
439
244
77
682
259
682
472
313
608
685
75
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