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.

495
206
283
655
513
755
765
606
649
819
106
609
282
944
723
546
988
754
888
462
743
164
453
173
14
453
706
358
959
330
795
207
273
854
132
388
206
301
263
711
875
737
151
396
564
172
759
94
737
332
555
810
546
577
90
950
292
343
996
351
353
763
824
549
821
286
363
183
393
388
55
310
291
359
338
754
90
889
917
698
494
284
302
220
104
731
724
830
922
94
904
555
58
438
460
135
835
605
706
900
BubbleSort - 0 steps
585
171
666
82
792
129
534
677
245
590
687
994
86
726
732
270
888
749
498
773
615
415
260
234
31
733
570
877
958
603
27
927
923
332
936
211
310
872
483
889
386
725
422
880
333
80
547
905
764
461
182
833
636
779
298
767
482
255
631
765
716
234
319
495
206
837
797
937
784
218
65
353
933
449
87
690
805
955
70
275
715
772
111
566
877
964
996
164
2
224
821
997
1
784
143
323
936
75
953
627
InsertionSort - 0 steps
989
966
692
436
649
281
61
69
923
209
853
544
305
118
693
742
477
550
121
245
770
610
458
30
833
582
545
441
247
818
699
897
694
754
303
407
880
753
964
112
142
373
491
829
994
979
262
503
509
439
843
83
548
602
99
781
120
933
7
582
515
301
717
656
61
73
846
932
320
875
956
244
487
174
378
206
813
767
231
128
423
917
133
68
105
277
924
614
859
866
541
293
965
785
621
937
848
258
403
511
ShellSort - 0 steps
720
318
730
532
696
628
290
470
192
514
611
481
740
856
283
899
229
675
854
252
608
94
408
704
887
202
317
864
920
204
808
357
496
502
617
11
428
144
280
250
186
574
831
518
728
94
40
335
30
19
160
126
851
186
688
358
135
6
915
230
615
11
580
692
479
44
669
967
666
378
927
985
301
695
440
508
473
44
909
28
546
630
774
239
817
726
790
936
153
411
330
242
840
802
747
511
453
833
282
697
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