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.

988
107
951
596
32
806
88
74
261
459
615
742
987
421
361
502
143
189
478
147
768
387
636
817
827
481
817
170
672
100
981
75
457
698
349
394
93
708
949
122
127
191
327
250
143
554
975
177
658
432
397
315
230
384
2
860
511
350
703
352
714
680
946
846
613
390
781
361
580
757
545
614
685
21
591
587
823
587
579
690
966
981
939
69
73
470
686
772
521
364
68
224
106
331
490
745
259
212
282
694
BubbleSort - 0 steps
326
221
706
373
801
643
387
491
89
490
359
844
393
402
934
517
19
907
764
918
811
273
760
189
868
75
475
228
394
329
272
299
535
756
193
793
324
662
270
821
56
991
650
914
465
291
790
925
503
234
754
940
115
761
545
255
450
581
190
194
581
832
593
267
996
578
245
371
867
116
529
405
553
279
698
601
129
545
869
125
971
647
682
433
79
885
237
842
688
654
524
679
76
287
946
953
197
18
615
510
InsertionSort - 0 steps
762
415
224
308
341
263
786
208
577
441
101
766
434
115
167
646
587
469
141
814
175
661
765
492
292
564
22
407
876
467
141
996
228
199
722
395
743
167
945
996
587
900
531
131
422
241
95
921
919
433
175
900
88
743
360
781
800
621
132
670
931
45
668
974
156
722
584
475
704
999
644
372
78
663
926
905
614
406
426
335
200
838
878
423
722
573
454
681
228
575
290
186
487
788
905
270
28
314
287
996
ShellSort - 0 steps
305
289
345
548
1000
155
61
387
225
925
655
186
248
658
995
790
731
1000
940
493
158
974
359
903
377
253
310
578
457
36
997
521
714
537
724
477
171
347
414
427
281
769
54
551
363
472
672
913
837
797
927
996
862
490
93
665
578
32
39
623
958
732
854
496
633
793
226
684
623
98
323
967
28
354
202
216
341
854
612
255
592
403
969
192
452
305
990
429
62
734
309
340
829
736
327
478
411
178
563
651
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