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.

284
921
828
27
109
953
593
625
776
149
526
817
195
519
439
338
890
222
281
61
387
930
161
9
714
8
183
848
735
592
788
107
974
337
861
621
839
82
932
706
226
715
624
233
367
716
923
959
471
259
12
801
606
119
814
510
463
817
739
914
572
540
888
509
592
285
534
703
845
546
316
524
517
218
633
524
380
171
553
188
340
655
610
188
268
517
113
962
65
906
667
576
844
6
229
153
56
549
89
487
BubbleSort - 0 steps
762
429
355
249
922
92
861
884
196
107
256
342
956
788
60
217
126
841
841
863
61
561
16
559
604
341
552
186
650
19
13
588
354
903
844
477
181
64
691
319
690
761
421
993
353
125
76
116
73
213
574
889
731
743
708
442
438
825
788
157
366
176
997
795
623
681
488
391
961
554
653
334
32
90
333
45
289
575
373
64
988
14
312
182
649
296
603
825
697
401
790
260
667
50
918
191
586
973
922
721
InsertionSort - 0 steps
443
614
896
379
641
157
854
91
43
665
259
737
487
930
140
746
229
494
508
758
332
357
359
915
935
240
364
137
520
829
33
694
42
286
285
466
64
812
820
787
18
150
665
672
519
386
61
963
973
746
487
864
597
433
407
719
322
601
397
294
975
834
361
634
978
675
929
944
872
976
364
143
159
668
197
442
334
921
847
249
486
928
634
52
621
345
16
532
161
93
776
847
157
596
391
605
876
791
838
848
ShellSort - 0 steps
821
812
454
770
562
252
122
53
568
151
427
388
439
184
923
561
575
526
596
652
377
649
850
361
258
178
594
160
959
259
195
491
907
210
277
513
530
990
587
715
286
370
258
720
435
569
851
673
228
652
725
611
343
82
329
156
710
813
995
131
284
73
268
400
564
786
520
489
215
522
996
52
505
115
440
840
421
93
866
188
328
93
155
459
656
212
871
159
42
380
774
69
667
493
207
548
270
572
559
126
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