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.

660
694
10
146
1
900
311
363
369
348
682
742
780
203
785
206
924
97
64
930
329
257
701
144
444
80
449
269
759
163
946
691
904
579
969
108
399
156
394
192
434
386
356
568
553
434
278
171
350
411
352
628
585
719
727
668
502
536
42
41
654
527
882
579
756
198
969
732
401
684
839
960
717
930
72
194
182
862
371
947
556
871
478
767
985
692
31
683
547
140
802
19
930
972
843
384
255
79
561
68
BubbleSort - 0 steps
265
425
61
555
133
860
995
619
486
962
412
842
513
724
571
815
459
352
28
883
551
837
374
325
465
836
271
474
60
851
800
249
9
922
806
288
775
189
274
296
740
959
906
149
103
728
375
47
664
774
222
217
706
697
113
382
283
998
779
194
497
335
732
790
635
275
899
781
527
339
586
197
412
810
324
349
909
652
137
567
718
345
958
435
878
993
510
731
517
542
976
88
230
452
31
838
879
877
649
355
InsertionSort - 0 steps
826
556
588
595
630
27
68
457
293
753
772
442
624
7
771
229
854
372
948
302
25
387
226
203
257
601
739
261
505
416
253
466
320
583
15
206
686
992
245
838
658
967
573
485
185
896
359
842
97
431
151
837
61
293
611
323
45
508
677
435
263
611
886
519
545
26
824
6
305
420
781
846
128
375
247
15
955
645
866
35
46
810
362
472
413
756
1
762
854
643
408
529
194
770
65
2
528
90
742
849
ShellSort - 0 steps
735
253
888
242
831
969
994
280
784
558
546
74
214
171
287
32
116
49
947
702
633
778
440
521
188
494
171
270
406
412
323
510
143
737
117
17
674
75
899
338
565
803
875
375
42
221
222
480
841
622
624
266
371
710
25
996
908
682
882
6
614
94
795
684
145
984
457
347
8
239
756
982
666
4
755
329
335
499
353
199
175
893
75
205
929
398
628
193
429
170
524
886
354
159
192
366
505
624
152
307
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