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.

889
441
520
962
243
447
818
218
468
129
316
972
681
234
677
159
878
366
574
884
705
484
959
135
27
788
471
994
710
988
142
722
632
469
273
735
749
424
245
408
151
934
438
818
518
372
370
880
831
631
345
650
617
703
70
321
323
491
922
898
477
476
382
933
924
795
908
963
148
582
549
179
711
808
288
261
865
520
100
70
845
509
476
87
70
130
532
309
438
183
458
269
277
858
81
96
918
796
75
220
BubbleSort - 0 steps
928
867
39
262
589
212
915
425
53
917
125
495
595
109
585
498
48
323
90
159
754
7
253
479
609
610
975
768
604
603
445
484
898
883
439
254
659
617
336
332
445
36
104
147
405
65
467
204
321
352
535
498
734
856
530
996
946
14
833
623
742
243
416
983
443
784
358
492
968
639
53
292
387
207
100
670
493
733
274
575
650
647
301
561
161
134
921
303
3
141
366
681
517
662
221
391
143
938
140
13
InsertionSort - 0 steps
168
11
541
89
379
325
96
826
153
369
687
486
372
210
429
497
236
10
846
264
834
741
708
6
50
174
904
167
68
264
557
973
113
391
710
461
523
206
975
520
115
283
347
196
566
467
232
952
414
327
969
777
198
972
361
5
288
203
932
559
903
462
154
495
711
602
949
233
987
312
562
387
836
217
943
587
544
396
812
749
645
69
61
261
671
710
133
411
557
304
162
475
271
878
293
204
748
873
179
856
ShellSort - 0 steps
211
671
974
157
849
632
793
63
325
120
594
639
117
873
753
614
110
103
404
454
308
194
577
739
728
534
165
660
438
996
441
237
810
614
153
70
863
675
877
914
647
892
205
794
318
661
553
119
924
728
579
334
92
722
348
946
703
587
449
441
958
676
295
5
300
364
413
466
13
9
543
504
702
504
421
795
752
396
717
727
757
916
356
656
873
577
17
728
924
516
621
167
873
344
187
607
544
426
714
833
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