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.

276
110
670
872
884
246
359
799
539
386
350
367
864
393
360
480
328
350
488
510
673
161
24
186
900
382
611
624
589
396
33
552
14
999
333
984
436
892
86
649
319
253
191
321
564
940
791
305
62
391
33
472
333
293
425
784
605
813
217
85
59
781
386
701
604
695
897
326
49
383
233
127
982
642
183
496
587
140
168
333
991
818
807
836
54
873
549
333
157
740
558
958
378
367
257
481
991
225
629
882
BubbleSort - 0 steps
853
533
493
299
581
630
314
492
723
786
565
331
233
413
915
950
252
120
605
61
903
571
546
672
84
257
660
630
886
490
948
358
975
558
206
357
68
319
262
450
305
546
213
665
374
862
247
750
102
355
369
454
188
62
757
752
799
162
196
232
93
817
310
921
822
296
574
584
84
37
200
231
666
990
332
204
72
596
424
710
179
798
116
804
758
791
710
984
476
582
58
319
84
761
146
571
898
206
494
836
InsertionSort - 0 steps
455
416
749
761
599
283
458
470
480
712
58
740
300
274
635
598
669
900
919
248
731
454
42
658
217
130
728
834
936
417
87
95
825
846
492
729
557
106
636
798
461
91
28
226
251
995
859
539
810
110
211
702
422
747
823
123
698
6
736
584
432
958
102
402
537
964
982
460
127
670
153
177
465
711
22
350
112
895
960
971
702
871
681
185
839
116
877
55
679
992
318
12
960
78
966
533
924
522
892
311
ShellSort - 0 steps
41
784
637
140
73
687
399
344
406
789
530
642
803
494
176
952
550
967
762
361
360
425
405
535
729
846
917
998
941
620
345
325
312
530
991
917
606
350
878
721
806
81
259
221
320
157
404
307
81
897
453
203
189
70
939
932
357
291
502
718
778
824
213
255
380
557
279
452
849
143
522
845
827
769
295
183
337
103
734
527
62
815
4
881
789
935
14
512
864
633
868
899
610
396
377
623
239
682
48
3
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