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.

66
864
474
887
426
990
933
342
405
270
566
636
2
874
642
437
436
305
748
691
290
885
62
469
972
549
191
292
165
103
240
30
593
839
908
479
18
769
807
532
257
206
380
817
790
78
565
204
820
378
39
358
433
344
117
881
943
897
585
458
98
581
16
367
603
284
758
50
982
217
115
654
416
311
125
186
788
609
790
669
121
652
552
587
906
734
605
585
992
624
672
430
554
97
89
232
287
23
276
692
BubbleSort - 0 steps
188
484
724
763
972
528
326
173
417
163
658
357
206
874
322
821
144
658
927
261
274
608
51
619
592
508
420
811
269
100
32
413
631
596
264
976
390
430
222
869
354
360
656
254
79
746
856
141
131
944
344
985
371
390
411
98
86
183
248
880
829
82
339
65
382
324
878
777
878
605
857
464
755
242
133
659
189
500
796
616
793
495
146
388
830
258
35
319
107
719
491
779
617
558
668
251
631
118
867
457
InsertionSort - 0 steps
377
148
853
233
57
968
277
425
640
490
92
522
168
49
13
941
900
36
560
432
540
952
753
238
257
202
616
955
566
182
43
718
273
54
647
829
899
901
939
81
928
780
472
32
115
313
478
751
790
961
911
166
475
378
568
352
863
396
482
291
766
225
127
336
711
674
734
13
464
437
686
791
272
787
579
593
129
583
525
415
329
894
813
292
916
868
604
777
764
677
763
657
548
643
683
213
494
187
675
315
ShellSort - 0 steps
463
229
216
450
938
65
299
797
447
258
961
772
332
984
368
956
989
284
738
939
274
58
298
255
822
885
467
675
38
427
716
999
48
957
770
397
488
731
520
853
92
722
557
154
488
631
584
756
585
413
779
251
580
825
629
27
731
690
127
27
487
758
367
338
789
585
752
144
244
490
87
465
7
861
302
358
846
570
534
804
38
972
861
964
501
764
749
812
652
661
868
210
789
50
127
520
131
371
947
610
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