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.

845
531
905
461
815
381
726
936
487
409
228
622
652
334
976
136
594
257
26
830
375
300
29
232
588
320
923
946
191
338
318
9
612
120
473
63
438
515
611
168
396
661
617
362
458
743
346
888
193
731
930
261
463
556
203
489
204
457
950
580
760
801
311
760
296
756
195
656
57
334
841
131
224
141
154
285
422
59
8
184
163
670
737
476
344
614
878
863
5
29
215
827
604
930
736
746
178
758
476
707
BubbleSort - 0 steps
794
381
497
276
686
71
866
359
641
88
894
137
184
45
687
335
106
137
768
628
217
154
222
701
319
80
82
132
874
362
610
628
654
30
31
130
849
909
752
70
292
806
79
448
454
527
286
349
901
834
684
26
552
379
915
652
989
73
51
550
157
427
403
714
959
49
75
514
66
983
925
84
581
306
67
790
20
331
792
396
775
411
904
239
212
612
689
713
120
986
880
306
375
198
714
391
177
794
543
640
InsertionSort - 0 steps
135
5
419
42
279
342
95
153
180
127
745
112
583
716
242
15
807
659
535
74
445
997
772
778
895
860
867
763
350
379
881
44
638
642
431
997
874
619
601
308
536
712
809
348
861
740
392
697
680
64
406
813
810
300
501
803
992
63
51
481
733
517
219
813
985
186
636
770
762
597
491
532
406
302
94
840
590
179
534
437
520
668
82
475
344
360
586
49
580
493
303
374
692
875
153
965
866
338
344
10
ShellSort - 0 steps
510
421
402
463
372
443
742
452
346
472
875
94
240
897
463
645
490
864
119
614
918
139
661
125
614
456
976
769
71
560
208
11
468
884
791
816
38
736
773
448
650
791
290
612
398
548
169
802
813
490
26
81
619
857
782
288
635
984
936
893
509
858
102
56
61
235
434
449
369
730
16
970
222
166
189
933
330
446
649
105
865
376
354
928
373
471
889
682
482
278
489
452
116
706
371
126
254
954
551
420
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