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.

299
645
693
207
725
836
727
743
371
478
283
362
835
481
572
587
799
415
70
631
517
990
256
323
15
520
861
919
27
365
964
493
667
658
269
88
488
358
163
216
670
541
870
172
398
198
813
124
432
952
193
999
237
679
668
448
28
800
796
3
745
81
170
15
394
147
881
352
245
608
538
137
86
359
179
70
399
309
536
669
866
399
795
389
487
735
903
840
487
498
638
527
20
717
314
919
418
122
134
804
BubbleSort - 0 steps
423
315
598
941
891
352
636
768
375
423
422
200
217
470
698
499
791
769
371
732
858
790
441
505
857
680
58
12
838
651
371
16
480
538
803
453
761
783
144
730
408
581
52
45
42
100
610
503
446
695
549
83
781
101
423
927
508
595
273
272
946
829
994
990
292
799
255
348
405
239
802
657
71
222
858
474
590
167
332
41
657
677
153
473
425
783
947
506
847
549
345
752
485
425
543
619
5
379
920
704
InsertionSort - 0 steps
22
139
987
976
480
857
139
753
373
95
211
235
398
309
950
290
594
146
207
587
608
555
747
774
40
948
17
920
542
424
380
519
997
16
430
838
728
38
796
346
846
474
194
978
660
816
384
66
820
703
321
753
227
313
258
162
317
713
683
293
69
341
331
85
382
40
928
2
192
937
392
852
581
567
276
97
822
913
182
760
269
705
333
566
637
754
925
234
237
718
818
260
770
789
659
150
748
449
586
255
ShellSort - 0 steps
631
413
845
813
709
690
560
251
453
840
642
403
780
926
606
867
37
347
766
921
628
306
310
50
921
415
334
376
844
641
926
460
166
211
741
990
586
681
430
944
729
432
450
460
899
425
414
393
192
689
242
704
618
987
812
232
707
912
792
422
302
631
463
632
386
977
447
504
664
453
945
937
893
912
501
357
439
930
17
184
404
353
663
566
632
71
340
471
174
485
839
28
661
648
26
128
543
595
300
199
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