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.

894
513
187
930
509
185
40
363
547
257
285
862
721
15
718
497
599
107
234
573
6
573
329
548
830
760
888
54
393
998
388
900
720
932
486
108
985
189
609
939
486
690
109
777
998
315
352
941
711
196
695
193
702
627
505
510
485
981
674
749
89
349
566
89
280
399
806
708
166
908
290
619
810
579
566
883
126
667
33
652
937
736
71
898
186
475
359
348
73
827
43
481
966
693
139
819
720
89
582
708
BubbleSort - 0 steps
629
666
947
250
734
350
487
323
323
877
987
815
6
212
370
1
605
478
450
493
575
105
937
324
364
504
342
176
292
45
339
940
397
686
822
228
793
922
835
140
685
284
728
109
366
915
791
758
241
663
896
272
289
538
837
916
729
317
291
718
948
438
503
74
408
744
404
438
238
808
380
720
947
967
116
826
584
763
867
348
419
456
11
771
470
314
820
595
95
395
698
910
505
782
78
140
890
333
387
650
InsertionSort - 0 steps
455
894
985
701
646
825
143
197
575
872
898
967
256
233
638
482
419
553
736
853
566
256
496
54
878
444
748
867
960
691
752
132
933
254
440
711
479
583
574
811
890
172
819
498
272
999
192
206
113
628
649
562
888
594
628
628
633
142
456
535
707
148
731
848
378
687
937
428
317
340
765
800
714
681
239
877
809
118
471
897
665
245
577
265
365
790
699
419
250
572
921
611
483
725
847
769
475
825
255
109
ShellSort - 0 steps
92
716
556
621
130
235
301
770
371
83
546
699
209
875
187
160
229
175
345
849
562
925
677
704
355
22
312
786
517
557
15
315
812
941
971
172
83
673
766
34
348
820
419
71
870
135
47
507
617
818
144
532
331
372
942
195
679
287
88
921
16
393
146
664
486
205
853
220
350
820
543
93
775
906
785
132
510
655
986
831
63
380
495
328
613
77
972
935
656
982
81
433
284
555
533
334
353
425
924
631
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