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.

183
108
389
259
443
668
925
266
575
440
955
930
612
473
25
512
914
595
551
91
716
930
549
484
31
434
491
297
353
694
62
916
584
635
293
140
750
753
88
672
185
463
150
116
201
735
370
776
45
553
788
333
166
634
381
934
886
772
199
428
194
642
662
393
68
341
97
122
753
364
362
583
697
352
440
385
702
624
647
839
386
468
294
390
511
815
986
932
89
540
414
840
25
793
816
423
59
525
81
9
BubbleSort - 0 steps
343
668
48
507
147
243
315
461
853
218
745
583
319
292
903
892
956
788
416
511
685
367
494
275
966
528
216
312
390
137
815
683
875
699
789
1000
464
233
544
673
648
643
931
701
417
727
923
895
921
945
796
39
873
522
152
357
371
327
558
811
226
892
586
592
733
237
515
47
479
555
280
734
714
696
278
408
813
522
692
627
796
127
984
646
862
272
620
613
930
408
458
732
318
118
517
304
952
674
901
634
InsertionSort - 0 steps
18
644
634
482
766
983
991
484
84
881
549
222
559
704
435
884
735
974
690
294
598
9
816
944
924
292
753
227
258
914
503
882
460
235
6
822
921
245
48
365
702
155
804
596
304
917
160
955
602
412
804
54
710
594
527
413
577
38
418
997
995
761
527
154
165
37
821
67
478
707
291
188
347
539
724
849
725
233
498
517
534
955
276
117
54
169
882
851
105
272
210
694
32
315
82
219
149
747
864
685
ShellSort - 0 steps
422
375
185
598
911
396
447
173
443
622
841
891
950
787
504
876
220
367
489
258
832
322
326
631
713
174
603
118
377
103
883
197
646
216
383
135
385
253
301
809
567
129
19
68
966
271
999
569
935
498
447
444
23
212
552
86
13
135
267
17
167
749
83
689
498
274
999
394
813
722
834
365
304
359
187
729
435
338
177
569
676
83
85
896
436
973
321
823
217
398
30
895
350
906
33
152
225
409
926
602
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